Python while语句之后的Else语句,以及字符串与字符串列表的比较?
我正在尝试创建一个小的测试脚本,在注释中添加一些内容。下面是我将在脚本中执行的主要函数。问题似乎是,当Python while语句之后的Else语句,以及字符串与字符串列表的比较?,python,if-statement,while-loop,conditional,Python,If Statement,While Loop,Conditional,我正在尝试创建一个小的测试脚本,在注释中添加一些内容。下面是我将在脚本中执行的主要函数。问题似乎是,当while块的计算结果为false时(也就是说,当它的计算结果不是这四个选项之一时),while块只是在无限循环中继续,我无法让else块运行。我还尝试在while循环中插入一个break,但这会在执行while循环后终止脚本 当while块的计算结果为false时,如何从while移动到else块?为什么我现在的做事方式不能如我所愿呢?谢谢 def start(): q01 = inp
while
块的计算结果为false时(也就是说,当它的计算结果不是这四个选项之一时),while
块只是在无限循环中继续,我无法让else
块运行。我还尝试在while循环中插入一个break
,但这会在执行while
循环后终止脚本
当while
块的计算结果为false时,如何从while移动到else
块?为什么我现在的做事方式不能如我所愿呢?谢谢
def start():
q01 = input("What is the subject of your note?\n")
q02 = input("Are you certain that the subject of your note is " + q01 + "?\n")
while q02 == 'No' or 'no' or 'NO' or 'n':
q01 = input("So, what is the subject of your note?\n")
q02 = input("Are you certain now that the subject of your note is " + q01 + "?\n")
else:
q03 = Enter("Enter the content of your note")
更改此声明
while q02 == 'No' or 'no' or 'NO' or 'n':
到
还有一种更优雅的方式:
def startMe():
q01 = input("What is the subject of your note?\n")
q02 = input("Are you certain that the subject of your note is " + q01 + "?\n")
negList = ['No', 'no', 'NO', 'nO', 'n', 'N'] # <<< Easily modifiable list.
while any(q02 in s for s in negList):
q01 = input("So, what is the subject of your note?\n")
q02 = input("Are you certain that the subject of your note is " + q01 + "?\n")
break:
q03 = input("Enter the content of your note")
def startMe():
q01=输入(“您的笔记主题是什么?\n”)
q02=输入(“您确定您笔记的主题是“+q01+”?\n”)
negList=['No','No','No','No','n','n'].\p>您的逻辑和代码是正确的,除了1个语法。
使用
而q02=='No'或q02=='No'或q02=='No'或q02=='n':
代替而q02==“否”或“否”或“否”或“n”:
您可以尝试:
def start():
q01 = input("What is the subject of your note?\n")
q02 = input("Are you certain that the subject of your note is " + q01 + "?\n")
while q02 == 'No' or q02 == 'no' or q02 == 'NO' or q02 == 'n':
q01 = input("So, what is the subject of your note?\n")
q02 = input("Are you certain now that the subject of your note is " + q01 + "?\n")
else:
q03 = input("Enter the content of your note")
start()
您的罪魁祸首是while循环条件:
while q02 == 'No' or 'no' or 'NO' or 'n':
这相当于:
while (q02 == 'No') or 'no' or 'NO' or 'n':
由于'no'
,'no'
和'n'
都是非空字符串,它们的计算结果为True
,因此您的条件计算结果为:
while (q02 == 'No') or True or True or True:
这显然总是正确的
要解决此问题,您需要将条件调整为:
while q02 == 'No' or q02 == 'no' or q02 == 'NO' or q02 == 'n':
尽管为了更具Python风格,您可以改为:
while q02 in ['No','no','NO','n']:
问题是while
块上的保护条件总是True
>>> q02 = 'y'
>>> q02 == 'No' or 'no'
'no'
或
运算符很有趣。它计算它的左操作数,如果它是“truthy”,则左操作数是运算的结果;否则,它将计算它的右操作数。在您的例子中,左操作数是一个布尔值(q02=='No'
的结果),右操作数是一个非空字符串。非空字符串是“truthy”,因此这就是结果
因此,q02=='No'或'No'或'No'或'n'
计算为True
当且仅当q02
为'No'
时。否则,它将计算为字符串'no'
,就循环而言,该字符串是“真实的”
>>> q02 = 'y'
>>> q02 == 'No' or 'no' or 'NO' or 'n'
'no'
>>> bool(q02 == 'No' or 'no' or 'NO' or 'n')
True
你能解释一下为什么我的方法不起作用吗?如果
注定要失败,那么没有的
方法就可以了。你能测试一下这段代码吗?因为我调用函数-1时似乎仍然卡在一个while循环中。(1) 否则
可以。(2) 虽然q02==“No”或“No”…
的语法没有错误,但它并没有达到提问者的预期。使用它没有多大意义,但也不违法。谢谢,这很有效,但为什么会出现这种情况?我按自己的方式操作时没有收到任何语法错误,因此为什么此方法会导致激活else块?根据接受的答案修复了while
/else
条件后,您使用while
应该可以工作。
>>> q02 = 'y'
>>> q02 == 'No' or 'no' or 'NO' or 'n'
'no'
>>> bool(q02 == 'No' or 'no' or 'NO' or 'n')
True