gpt4 book ai didi

python - 重新启动 if then 语句

转载 作者:行者123 更新时间:2023-11-28 22:56:36 30 4
gpt4 key购买 nike

就像我在 python 中一样。

choice1 = raw_input('John Blue Green')
if choice1 == 'A':
print('blah')
elif choice1 == 'B':
print('blahblah')

有人输入 B 但它不正确,所以我希望它返回并再次询问。我怎么做?谨防,我是一个编程小白。

最佳答案

你基本上需要循环这个。一个例子是将其置于无限循环中,并在达到所需结果时手动中断它:

while True:
choice1 = raw_input('John Blue Green')
if choice1 == 'A':
print('blah')
break # <-- 'A' is okay, so we can get out of the loop then
elif choice1 == 'B':
print('blahblah')

根据您的情况,您当然可以调整循环条件中的 True 以使其不是无止境的,而是实际对用户输入使用react。然后你就不需要 break 了,但是循环会自然地停止循环。但是,如果您有多个接受输入值,那么仍然使用中断而不是使用巨大的循环条件可能会更清晰。

关于python - 重新启动 if then 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15342790/

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