我一直在尝试在 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”时它不会破坏程序,因为它会使我们陷入外循环。那么如何解决这个问题呢?