Python 如果存在';什么是循环中的循环?
我一直在尝试在Python上制作对数计算器。我离完成它只有一步之遥。代码如下:Python 如果存在';什么是循环中的循环?,python,loops,Python,Loops,我一直在尝试在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(i
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”时,它不会中断程序,因为它会将我们置于外部循环中。
那么如何对其进行排序呢?您可以在值之外定义一个布尔参数,初始值为“false”。在外部循环的每次运行开始时,可以检查此布尔值,如果为真,则还可以断开外部循环。之后,当您想要在内部循环中结束外部循环时,只需在中断内部循环之前使该值为真即可。这样,外部循环也将中断。您可以在值之外定义一个布尔参数,初始值为“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
请注意,如果用户继续输入错误的输入,则可以进入无限循环。您可以限制他最多尝试几次。您可以将该测试移动到其他功能:
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
请注意,如果用户继续输入错误的输入,则可以进入无限循环。您可以限制他最多尝试几次。您可以这样做:
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
您可以这样做:
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
试试这个:
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.")
试试这个:
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')
如果用户输入的数字小于或等于零,这将产生一个您未考虑的异常 至于打破循环,它不是太嵌套,您不能已经打破。如果您有更深的嵌套,那么我建议使用以下模板来打破嵌套循环
def loopbreak():
while True:
while True:
print('Breaking out of function')
return
print('This statement will not print, the function has already returned')
如果运算符中的
太高级,总是会写出布尔逻辑:如果response==“y”或response==“y”
等待,不是(“y”或“y”)
总是True,那么如果response==“y”或“y”):
实际上是如果response==True:
,这总是False?@Volatility。啊!!等待你的确有括号。我没看见。抱歉,我将更新答案。但是,尽管这可能有效,但令人困惑,所以最好在
操作符中使用。@RohitJain lol,我听起来像是这里的OP吗?我只是想指出这一点<代码>;)代码>@Volatility。古尔。是的,我很困惑。谢谢你的指点。:)如果
运算符中的太高级,总是会写出布尔逻辑:如果response==“y”或response==“y”
等待,不是(“y”或“y”)
总是True,那么如果response==“y”或“y”):
实际上是如果response==True:
,这总是False?@Volatility。啊!!等待你的确有括号。我没看见。抱歉,我将更新答案。但是,尽管这可能有效,但令人困惑,所以最好在
操作符中使用。@RohitJain lol,我听起来像是这里的OP吗?我只是想指出这一点<代码>;)代码>@Volatility。古尔。是的,我很困惑。谢谢你的指点。:)loopbreak只会从自身返回,它是一个模板。你有没有读过“如果你有一个更深的巢穴,那么我会建议如下。”从这个角度来看,你的评论对我来说没有意义。我不会在这段代码中使用双while循环,但我想回答一个一般性的问题。如果您处于嵌套循环中,并且希望从中中断,return仍然会终止整个函数,而不仅仅是循环。这可能是你想要的,但可能不是。公平地说,您确实说您正在中断函数…loopbreak将从自身返回。它的意思是作为一个模板。你有没有读过“如果你有一个更深的巢穴,那么我会建议如下。”从这个角度来看,你的评论对我来说没有意义。我不会在这段代码中使用双while循环,但我想回答一个一般性的问题。如果您处于嵌套循环中,并且希望从中中断,return仍然会终止整个函数,而不仅仅是循环。这可能是你想要的,但可能不是。公平地说,您确实说您正在中断函数…谢谢,尽管第一行中不需要stop=False
。非常感谢。不管怎样,这是最有效的解决方案。@PreetikaSharma,是的,现在看起来是这样。但是我开始的时候脑子里还想着别的东西,所以它就在那里初始化了。谢谢,尽管在第一行中不需要stop=False
。非常感谢。不管怎样,这是最有效的解决方案。@PreetikaSharma,是的,现在看起来是这样。但是我开始的时候脑子里还想着别的东西,所以它就在那里初始化了。response==(“y”或“y”)
看起来不太对劲。您希望在“Yy”中使用响应响应==(“y”或“y”)
看起来不正确。您希望在'Yy'中得到响应
。