你.lower
应该是.lower()
。.lower()
是一个函数,不带括号调用它不会做任何事情:
>>> string = 'No'
>>> string.lower
<built-in method lower of str object at 0x1005b2c60>
>>> x = string.lower
>>> x
<built-in method lower of str object at 0x1005b2c60>
>>> x = string.lower()
>>> x
'no'
>>>
此外,您正在检查replay == input(...)
. 你只想要一个=
分配:
>>> result = 0
>>> result == 4
False
>>> result = 4
>>> result == 4
True
>>>
在第二个循环中,您也有一个不需要的:
after 。print replay
while True
这是一个建议:不要使用replay in ("no, "n")
非常unpythonic的 using ,而是使用内置方法startswith(char)
查看它是否以该字符开头:
>>> string = "NO"
>>> string.lower().startswith("n")
True
>>> string = "yeS"
>>> string.lower().startswith("y")
True
>>>
这也适用于naw
, oryeah
等输入。
这是您编辑的代码:
a = int(input("Enter the first number :"))
b = int(input("Enter the second number :"))
print("sum ---" + str(a+b))
print("difference ---" + str(a-b))
print("product ---" + str(a*b))
print("division ---" + str(a/b))
input()
while True:
print("Do you want to try again?")
while True:
replay = input("Do another Calculation? 'y' for yes. 'n' for no.").lower()
print(replay)
if replay.startswith('y') == True:
break
if replay.startswith('n') == True:
exit()
else:
print("I don't understand what you wrote. Please put in a better answer, such as 'Yes' or 'No'")