我是一個整體編程的新手,並試圖在python中編程計算器。任何人都知道爲什麼我的代碼不打印任何東西?
calc_version = input("Please choose a calc type (1/2)")
if calc_version == 1:
calc_type = input("Enter Function (Add/Sub/Div/Mult) ")
number1 = input("Please enter the first number ")
number2 = input("Please enter the second number ")
if calc_type == "Add":
Sum = float(number1) + float(number2)
if calc_type == "Sub":
Sum = float(number1) - float(number2)
if calc_type == "Div":
Sum = float(number1)/float(number2)
if calc_type == "Mult":
Sum = float(number1) * float(number2)
print("The sum of" ,number1, "and" ,number2, "is" ,Sum,)
if calc_version == 2:
number1 = input("Please enter the first number ")
operator = ("Please choose an operator (+,-,*,/)")
number2 = input("Please enter the second number ")
if operator == "+":
Sum = float(number1) + float(number2)
if operator == "-":
Sum = float(number1) - float(number2)
if operator == "/":
Sum = float(number1)/float(number2)
if operator == "*":
Sum = float(number1) * float(number2)
print("The sum of" ,number1, "and" ,number2, "is" ,Sum,)
這是工作的罰款,直到我說的第一下的第二版,calc_version = input("Please choose a calc type (1/2)"
線似乎已經不惜一切打破了我的整個代碼如無打印。 如果有人能告訴我我做錯了什麼,這將是一個很大的幫助。
'print(repr(calc_version))' - 它是什麼?不是'1'或'2',但可能是'1'或'2'(一個字符串)。 – deceze
命名差異,商和產品'Sum'是變量名的奇怪選擇。爲什麼不'result'? –
很可能你使用的是python 3(括號內的打印),其中input()返回str,而不是int。並且你正在比較str == int – abccd