我一直在試圖用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