简易计算机python实现

def Calculation():
    number_str=""
    print "欢迎您使用光荣之路计算机"
    while 1:
        print "退出系统请按回车键"
        operation=raw_input("请输入您的下一步操作: ")
        if operation=="":
            operation=raw_input("您确定要退出系统吗,确定请按数字'1': ")
            if operation=="1":
                print "退出成功"
                break
        elif operation in ["+","-","*","/","//"]:
            if len(number_str)==0:
                number_str+=operation
            else:
                if number_str[-1] in ["+","-","*","/","//"]:
                    print "您已经连续输入两次计算符号,请您重新输入要计算的数字"
                else:
                    number_str+=operation
        elif operation.isdigit():
            if len(number_str)==0:
                number_str+=operation
            else:
                if number_str[-1].isdigit():
                    print "您已经连续输入两次计算符号,请您重新输入要计算的数字"
                else:
                    number_str+=operation
        elif operation=="=":
            if number_str=="":
                print "您要计算得内容为空,请输入内容"
            elif number_str[-1] in ["+","-","*","/","//"]:
                print "您要计算的内容最后内容为空,请您输入计算内容"
            else:
                try:
                    if number_str[0].isdigit():
                        print number_str+"计算结果为:"+str(eval_r(number_str))
                    else:
                        print "0"+number_str+"计算结果为:"+str(eval_r(number_str))
                except Exception:
                    print number_str+"计算过程中出现错误:"+e.message

        else:
            operation=raw_input("您输入有误,请重新输入: ")
posted @ 2019-07-14 21:42  张特别  阅读(1272)  评论(0编辑  收藏  举报