[英]python name not defined even though it is
I know this is basic but i actually don't even know what I've done wrong. 我知道这是基本知识,但实际上我什至不知道自己做错了什么。
while True:
try:
end=int(input("If You Dont Want To Buy Anything Press 1 To Exit\nOr If You Would Like To Try Again Please Press 2"))
except ValueError:
print("\nPlease Enter Only 1 Or 2")
if end==1:
exit()
elif end==2:
continue
I have literally defined end at the start and yet the error is NameError: name 'end' is not defined
I've even tried making end a global. 我已经从头开始定义了end,但是错误是NameError: name 'end' is not defined
我什至尝试了将end NameError: name 'end' is not defined
全局。
end
is only assigned to if there was no ValueError
. 仅当没有ValueError
时才将end
分配给。 If int()
raises an exception, then the assignment never takes place . 如果int()
引发异常,则分配永远不会发生 。
Either test for valid end
values inside the try
(so that you know no exception was raised), or assign a default value to end
first. 要么在try
内测试有效的end
值(这样您就不会引发异常),要么为默认值指定第一个end
。
For example, the following will not throw your exception and still prompt the user to re-enter the number if anything other than 1
or 2
was entered: 例如,以下内容不会引发您的异常,并且如果输入的不是1
或2
则仍然提示用户重新输入数字:
while True:
try:
end=int(input("If You Dont Want To Buy Anything Press 1 To Exit\nOr If You Would Like To Try Again Please Press 2"))
if end==1:
exit()
elif end==2:
break
except ValueError:
pass
print("\nPlease Enter Only 1 Or 2")
Note that I moved the print()
to be outside the except
block; 请注意,我移动的print()
是外侧的except
块; it'll be printed if an exception was thrown or when no break
or exit()
was executed. 如果抛出异常,或者没有执行break
或exit()
,它将被打印出来。 Note that I used break
here instead of continue
to exit the while True
loop; 注意,我在这里使用break
而不是continue
退出while True
循环; continue
would just start the next iteration, prompting the user to enter a number again. continue
,则只会开始下一个迭代,提示用户再次输入数字。
Others have explained the problem; 其他人解释了这个问题。 here's a canonical solution. 这是一个规范的解决方案。 This matches the way many of us regard the process: - grab a response - until I get a legal response - ... tell the user what's wrong - ... get a new response 这与我们许多人对待流程的方式相符:-抓取答复-直到得到合法答复为止-...告诉用户出了什么问题-...得到新的答复
input_prompt = "If You Don't Want To Buy Anything Press 1 To Exit\nOr If You Would Like To Try Again Please Press 2"
response = input(input_prompt)
while response != '1' and response != '2':
print("\nPlease Enter Only 1 Or 2")
response = input(input_prompt)
end = int(reponse)
This has no unnatural loop exits (harder to follow and maintain), and no exception processing (slow). 这没有异常退出(更难跟踪和维护),也没有异常处理(缓慢)。
它尝试为end分配一个值,但是捕获到ValueError,并且在except之后尝试查看“ end”是什么,但是由于异常,它从未被分配任何值。
If int(input(...))
fails, a ValueError
is raised. 如果int(input(...))
失败,则会引发ValueError
。 That happens before end
is assigned. 这发生在分配end
之前。 Add another control statement in the error handling, for instance 例如,在错误处理中添加另一个控制语句
while True:
try:
end=int(input("If You Dont Want To Buy Anything Press 1 To Exit\nOr If You Would Like To Try Again Please Press 2"))
except ValueError:
print("\nPlease Enter Only 1 Or 2")
continue # ask again
if end==1:
exit()
elif end==2:
continue
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.