If语句不工作-必须按两次enter



这是一个代码片段,我正在工作,但我真的卡住了为什么它不工作

if语句(yes)工作完美,但是当我输入除yes以外的任何内容时,elif和else不会抛出错误或注册任何内容。

while current_room == 'Bathroom':
if 'Lavender Oil,' not in inventory:
print('Wow, this bathroom has so many bottles of stuff!')
print('Do you want to investigate the shelf of bottles?')
if input().strip().lower() == 'yes':
print('yes')
elif input().strip().lower() == 'no':
print('no')
else:
print('else')

根据你的描述,我猜有一点,但我认为问题是:

if input().strip().lower() == 'yes':
print('yes')
elif input().strip().lower() == 'no':
print('no')

不同于

cmd = input.strip().lower()
if cmd == 'yes':
print('yes')
elif cmd == 'no':
print('no')

具体来说,在您尝试的代码中,它要求用户输入两次,一次在原始的if子句中,第二次在elif子句中。这意味着你比较的东西不稳定。解决方案是存储用户响应,然后将其与可用选项进行比较。

最新更新