如果循环中存在循环,如何中断程序?
我一直在试图用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”时它不会中断程序,因为它会将我们置于外部循环中。 那么如何整理呢?
你可以做这样的事情:
stop=False
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?")
while True:
response = input("Hit y for yes or n for no\n")
if response == ("y" or "Y"):
stop = False
break
elif response == ("n" or "N"):
stop = True
break
else:
continue
if stop:
break
except ValueError:
print("Invalid Input: Make sure your number
您可以在初始值为'false'的值之外定义一个布尔参数。在外循环的每次运行开始时,您可以检查此布尔值,如果它为真,那么也会打破外循环。在此之后,当你想要在内部循环内部结束外部循环时,只需在打破内部循环之前使值为真。这种方式外环也将打破。
您可以在测试移动到不同的功能:
def read_more():
while True:
print("Want to check another one?")
response = input("Hit y for yes or n for no\n")
if response == ("y" or "Y"):
return True
elif response == ("n" or "N"):
return False
else:
continue
然后在你的函数,只是测试这种方法的返回类型:
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)
if read_more():
continue
else:
break
需要注意的是,你可以进入无限循环,如果用户继续输入错误的输入。你可以限制他达到一些最大的尝试。
如果'in'运算符太高级,总会写出布尔逻辑:'if response ==“y”or response ==“Y”' – lowerkey 2013-02-10 09:57:16
等待,不是'(“y”或“Y “)总是真的,所以如果response ==(”y“或”Y“):'实际上是'if response == True:',这总是False? – Volatility 2013-02-10 10:02:23
@挥发性。啊!等待。你那里有括号。我没看见。对不起会更新答案。但是,尽管这可能会起作用,但是令人困惑,所以最好使用'in'运算符。 – 2013-02-10 10:07:59
试试这个:
import math
class quitit(Exception):
pass
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?")
while True:
response = input("Hit y for yes or n for no\n")
if response == ("y" or "Y"):
break
elif response == ("n" or "N"):
raise quitit
except quitit:
print "Terminated!"
except ValueError:
print("Invalid Input: Make sure your number is greater than zero and no alphabets. Try Again.")
如果用户输入小于或等于零的数量,这将产生一个异常,你有没有占了。
只要突破循环,它不会嵌套,你不能已经爆发。如果你有更深的嵌套,那么我会建议使用下面的模板来打破嵌套循环。
def loopbreak():
while True:
while True:
print('Breaking out of function')
return
print('This statement will not print, the function has already returned')
stop=False
while not stop:
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?")
while True:
response = raw_input("Hit y for yes or n for no\n")
if response == ("y" or "Y"):
break
elif response == ("n" or "N"):
stop = True
break
except ValueError:
print("Invalid Input: Make sure your number")
'响应==( “Y” 或 “Y”)'不看的权利。你想在'Yy''上做出回应。 – georg 2013-02-10 11:06:37