gpt4 book ai didi

python - 在Python中使某个语句永远循环

转载 作者:行者123 更新时间:2023-12-01 04:56:10 27 4
gpt4 key购买 nike

while 1 == 1:
do = raw_input('What would you like to do?')

在上面的示例中,您可以看到代码旨在使某些内容永远循环,例如:

if do == 'x':
print 'y'
elif do == 'z':
print 'a'

因此,这个 if 语句已经执行,我希望再次执行 raw_input ,以便该人可以输入其他内容,并且程序再次继续。

我不想把整个程序放在一个

while True:

程序或

while 1 != 2:

声明。

最佳答案

通常,您会执行此操作,直到满足特定条件为止,例如,用户输入 q 退出;否则它只是一个无限循环,您需要强制退出整个程序。

试试这个逻辑:

result = raw_input('What would you like to do? Type q to quit: ')

while result.lower() != 'q':
if result == 'x':
print 'y'
if result == 'z':
print 'a'
result = raw_input('What would you like to do? Type q to quit: ')
print('Quitting. Good bye!')

关于python - 在Python中使某个语句永远循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27340036/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com