题解 | #简单计算器#

简单计算器

http://www.nowcoder.com/practice/b8f770674ba7468bb0a0efcc2aa3a239

python3 直白解放,其他实在想不出来了。
s = input()
for i in range(len(s)):
    if s[i] in ("+","-","*","/"):
        re = s.split(s[i])
        if s[i] == "+":
            pls = float(re[0])+float(re[1])
            print("%.4f"%float(re[0])+s[i]+"%.4f"%float(re[1])+"="+"%.4f"%pls)
        elif s[i] == "-":
            pls = float(re[0]) - float(re[1])
            print("%.4f" % float(re[0]) + s[i] + "%.4f" % float(re[1]) + "=" + "%.4f" % pls)
        elif s[i] == "*":
            pls = float(re[0]) * float(re[1])
            print("%.4f" % float(re[0]) + s[i] + "%.4f" % float(re[1]) + "=" + "%.4f" % pls)
        elif s[i] == "/":
            if re[1] == "0.0":
                print("Wrong!Division by zero!")
            else:
                pls = float(re[0]) / float(re[1])
                print("%.4f" % float(re[0]) + s[i] + "%.4f" % float(re[1]) + "=" + "%.4f" % pls)
    else:
        if s[i] not in ("+","-","*","/","0","1","2","3","4","5","6","7","8","9","."):
            print("Invalid operation!")
            break


全部评论

相关推荐

最讨厌装boyi的二🔥:服从性测试😉
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务