我在试着做一个蟒蛇计算器。但出于某种原因,它只会输出平方根选项。
我目前在我的iPhone上使用python3.4和3.3模拟器,但在这两种设备上都存在相同的问题。
print("This program is a calculator app \n Creator: AtPython")
print("...")
Print("please choose a mathematical operation")
Opt = input("square-root. \n addition, \n subtraction, \n multiplication, \n division: ")
if opt == "square-root" or "squareroot":
num = float(input('Enter a number: '))
num_sqrt = num ** 0.5
print('The square root of %0.3f is %0.3f'%(num ,num_sqrt))
input("Press 'Enter' to exit")
If opt == "addition":
add_1 = float(input("Please enter a number"))
add_2 = float(input("Please enter another number"))
add_ans = add_1 + add_2
print(add_ans)
qus = input("Would you like to add another number? [y/n]")
elif qus == "y":
add_3 = float(input("Enter a third number"))
input("Press 'Enter' to exit")
else:
input("Press 'Enter' to exit")发布于 2015-07-04 16:50:25
if opt == "square-root" or "squareroot":这不解析为if opt == "square-root" or opt == "squareroot":,此解析为if (opt == "square-root") or bool("squareroot"),这将返回True,因为调用字符串上的bool总是True,除非字符串是空的。(更多信息请参见链接 )
解决办法是:
if opt == "square-root" or opt == "squareroot":或
if opt in ["square-root","squareroot"]:https://stackoverflow.com/questions/31222934
复制相似问题