我一直在试图用Python做一个对数计算器。我离完成它只有一步之遥。下面是代码:如果循环中存在循环,如何中断程序?
import math
print("Welcome to logarithm calculator")
while True:
try:
inlog = int(input("Enter any value greater than zero to lookup its logarithm to the base 10\n"))
outlog = math.log(inlog, 10)
print(outlog)
# Here, the program will ask the user to quit or to continue
print("Want to check another one?")
response = input("Hit y for yes or n for no\n")
if response == ("y" or "Y"):
pass
elif response == ("n" or "N"):
break
else:
#I don't know what to do here so that the program asks the user to quit or continue if the response is invalid?
except ValueError:
print("Invalid Input: Make sure your number is greater than zero and no alphabets. Try Again.")
else语句后,我希望程序要求用户一次又一次地做出反应,直到它为“Y”或“Y”和“N”或有效的反应“ N”。如果我在这里添加另一个while循环,如果用户输入“y”,那么对于pass语句会很有帮助。但是当用户响应为“n”时它不会中断程序,因为它会将我们置于外部循环中。 那么如何整理呢?
'响应==( “Y” 或 “Y”)'不看的权利。你想在'Yy''上做出回应。 – georg 2013-02-10 11:06:37