我正在尝试制作一个python计算器。但由于某种原因,它只会输出平方根选项。另外,如果您有任何有助于改进我的代码的提示,请提供。 我目前在我的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")
答案 0 :(得分:0)
if opt == "square-root" or "squareroot":
这不解析为if opt == "square-root" or opt == "squareroot":
这解析为if (opt == "square-root") or bool("squareroot")
,返回True
因为对字符串调用bool
总是True
,除非字符串是空。 (有关详细信息,请参阅link)
修复将是:
if opt == "square-root" or opt == "squareroot":
或
if opt in ["square-root","squareroot"]: