我正在制作一个计算器程序,它具有平方根功能,但首先,您需要输入“s”才能访问它,我想制作它以便用户可以输入“S”或“s”并拥有计算机仍然可以识别它并调出平方根选项,但是如果我添加 s.upper() 和 s 变量,它可以工作但不是预期的代码是:
import math
def calculator():
while True:
s = "s"
intro = input('Hello! Please type * for multiplication, / for division, + for addition, - for subtraction, ** for exponents, and "s" for square root \n')
if intro not in ["*", "/", "+", "-", "**", s.upper(), s]:
print ("that wasnt an option!")
continue
if intro != s:
num1 = int(input("Whats your first number \n"))
num2 = int(input("Whats your second number \n"))
if intro != s.upper():
num1 = int(input("Whats your first number \n"))
num2 = int(input("Whats your second number \n"))
if intro == "*":
print(num1 * num2)
break
elif intro == "/":
print(num1/num2)
break
elif intro == "+":
print(num1 + num2)
break
elif intro == "-":
print(num1 - num2)
break
elif intro == "**":
print(num1 ** num2)
break
elif intro == s.upper():
num_sqr = int(input("What number do you wanna find the square root of \n"))
print(math.sqrt(num_sqr))
break
elif intro == s:
num_sqr = int(input("What number do you wanna find the square root of \n"))
print(math.sqrt(num_sqr))
break
calculator()
每当用户键入 s 时,它都会忽略变量 num1 和 num2 ,因此它可以运行 num_sqr 变量,因此输出为:
s (or S)
Whats your first number
2
Whats your second number
4
What number do you wanna find the square root of
24
4.898979485566356
而不是:
s (or S)
What number do you wanna find the square root of
24
4.898979485566356
为什么会这样,我该如何解决?