正在尝试使用python中的集合退出。我做错了什么?
我试着用exit中的单词退出,我试着做一个程序,你可以输入一个整数或字符串,如果num在exit中,它将退出程序正在尝试使用python中的集合退出。我做错了什么?,python,python-3.x,Python,Python 3.x,我试着用exit中的单词退出,我试着做一个程序,你可以输入一个整数或字符串,如果num在exit中,它将退出程序 loop = True status = True exit = set(["exit","leave"]) while loop: while status: print ("Any of these commands will quit the porgram",exit) num = input("Pick a number: ")
loop = True
status = True
exit = set(["exit","leave"])
while loop:
while status:
print ("Any of these commands will quit the porgram",exit)
num = input("Pick a number: ")
if 1 == 2:
print ("What the hell, how does 1 = 2")
elif num in exit:
loop = False
status = False
else:
continue
这里有一段代码,至少是与这个问题相关的一段代码,需要进行一些额外的编辑。如果需要更多信息,请询问
这是输出
Any of these commands will quit the porgram {'exit', 'leave'}
Pick a number: exit
Any of these commands will quit the porgram {'exit', 'leave'}
Pick a number: leave
Any of these commands will quit the porgram {'exit', 'leave'}
Pick a number: Exit
Any of these commands will quit the porgram {'exit', 'leave'}
Pick a number:
它只是永远循环你的缩进是错误的。您正在循环和收集输入,但没有运行if语句。请尝试以下方法:
while loop:
while status:
print ("Any of these commands will quit the porgram",exit)
num = input("Pick a number: ")
# Note extra indents below here
if 1 == 2:
print ("What the hell, how does 1 = 2")
elif num in exit:
loop = False
status = False
else:
continue
您没有将测试置于while状态:循环中。您只需缩进这些行:
loop = True
status = True
exit = set(["exit","leave"])
while loop:
while status:
print ("Any of these commands will quit the porgram",exit)
num = input("Pick a number: ")
if 1 == 2:
print ("What the hell, how does 1 = 2")
elif num in exit:
loop = False
status = False
else:
continue
事实上,您的if语句没有被检查,因为Python仍然在处理while status:loop。我认为程序是正确的,您所需要的只是适当的缩进,以便条件子句在while status look中,并且可以本质上更改status的值以帮助它实际退出循环,我想知道为什么你需要两个循环,一个嵌入另一个
loop = True
status = True
exit = set(["exit","leave"])
while loop:
while status:
print ("Any of these commands will quit the program", exit)
num = input("Pick a number: ")
if 1 == 2:
print ("What the hell, how does 1 = 2?")
elif num in exit:
loop = False
status = False
else:
continue
您的while状态:循环永远不会退出,因为状态永远不会更改。你的意思是在代码后面缩进代码,把它放在循环中吗?首先感谢你的帮助,是的,这就是我的意思,我以前让它工作过,我让我的一个同学看到了代码,我想向他展示它是如何运行的,所以我们中的一个人一定是无意中看到的。现在我试了一下,结果不一样了。在我能够使用Exit退出之前,set将保持不变。不是很重要,但是如果我能得到一个关于如何做到这一点的答案,我将不胜感激。我的意思是不区分大小写。很抱歉,我应该从这个例子中删除其中一个循环,但第二个循环是用于其他东西的。