python Calculator:检查输入有效性

时间:2014-03-18 03:00:16

标签: python

我的代码似乎工作正常,除了它不打印除例外: 打印("此输入无效。")部分。当我试图解决它时,情况变得更糟。有谁可以帮助我,我错过了什么?谢谢!

示例输出正确

Calculator
Give a number: hah, NEVER
This input is invalid.
Give a number: What?
This input is invalid.
Give a number: 100
Give a number: Just kidding
This input is invalid.
Give a number: 50
(1) +
(2) -
(3) *
(4) /
(5)sin(number1/number2)
(6)cos(number1/number2)
(7)Change numbers
(8)Quit
Current numbers: 100 50
Please select something (1-6): 2
The result is: 50
(1) +
(2) -
(3) *
(4) /
(5)sin(number1/number2)
(6)cos(number1/number2)
(7)Change numbers
(8)Quit
Current numbers: 100 50
Please select something (1-6): 8
Thank you!

我的代码:

def getnumber():
    while True:
        try:
            number = input("Give a number: ")
            if number.isdigit():
                return number

        except Exception:
            print("This input is invalid.")

def main():
    import math
    print("Calculator")
    promptForNumbers = True

    while True:

        if promptForNumbers:


            number1 = int(getnumber())

            number2 = int(getnumber())

            promptForNumbers = False
        print("(1) +\n\n(2) -\n\n(3) *\n\n(4) /\n\n(5)sin(number1/number2)\n\n(6)cos(number1/number2)\n\n(7)Change numbers\n\n(8)Quit\n")
        print("Current numbers: %s %s" % (number1, number2))
        selection = int(input("Please select something (1-8): "))        


        if selection == 1:
            print("The result is: %s" % (number1 + number2))
            print("\n")
        elif selection == 2:
            print("The result is: %s" % (number1-number2))
            print("\n")
        elif selection==3:
            print("The result is: %s" % (number1*number2))
            print("\n")
        elif selection==4:
            print("The result is: %s" % (number1/number2))
            print("\n")
        elif selection==5:
            print("The result is: %s" % math.sin(number1/number2))
            print("\n")
        elif selection==6:
            print("The result is: %s" % math.cos(number1/number2))
            print("\n")
        elif selection==8:
            print("Thank you!")
            break
        elif selection==7:
            promptForNumbers = True
        else:
            print("Selection was not correct.")  
if __name__ == "__main__":
    main()

3 个答案:

答案 0 :(得分:1)

您没有看到执行异常语句的原因是,try块中的代码不会引发异常。但是,对于int的构造函数,如果传递的值不是数字,则会引发ValueError异常。

根据我的理解,尝试转换,然后捕获异常是在这种情况下验证输入的最pythonic方式。您可以在docs中阅读有关Python中异常的更多信息。

def getnumber():
    while True:
        try:
            return int(input("Give a number: "))
        except ValueError:
            print("This input is invalid.")

您需要确保从main中调用getnumber的所有调用中删除对int的调用。还有,最后一点说明。将导入语句置于文件顶部无意中被认为是正确的形式。如果您想了解更多信息,可以在PEP8了解相关信息。

答案 1 :(得分:0)

试试这个:

def getnumber():
    while True:
        number = raw_input("Give a number: ")

        if number.isdigit():
            return number
        else:
            print("This input is invalid.")

答案 2 :(得分:0)

    if number.isdigit():
            return number

    else:

        raise ValueError('Not a valid input value.')