为什么我的嵌套 while 循环没有按预期工作?

问题描述 投票:0回答:1

我正在为大学编写一个基于文本的游戏。我得意忘形,超越了一切。当我意识到代码的 take_item 部分不起作用时,我以为我要结束了,因为我使用了所有 IF 语句,所以我将 if user input is not equal to 'yes' or 'no' 更改为 while 循环打印无效输入消息,然后提示用户再次输入。问题是,无论我现在输入什么,包括“是”和“否”,它都会打印无效消息并停留在循环中。

I have excluded my dictionary and other stuff and only included my code from the initial game loop to almost the end of it. I have capitalized the nested while loops in question to make them easier to find.


while user_command != 'Exit':
    #if statement for valid commands
    if user_command in move_direct:
        user_command = user_command.replace('Go ', '')
        if user_command in rooms[current_room]:
            os.system('cls')
            current_room = rooms[current_room][user_command]
            if boss_room == current_room:
                os.system('cls')
                print('you encountered the kracken without all items, you lose')
                exit()
            print('You have entered the {}.'.format(current_room))

            if 'item' in rooms[current_room]['other']:
                rooms[current_room]['other']['count'] += 1
                if rooms[current_room]['other']['count'] == 1:
                    print()
                    print('{}\n'.format(rooms[current_room]['other']['descript1']))
                    if len(inventory) == 0:
                        print("Your inventory: [empty]\n")
                    else:
                        print('Your inventory:', ', '.join(inventory))
                        print()

                    take_item = input('Would you like to retrieve the item? Type: yes or no\n').lower()
                    WHILE take_item != 'yes' or 'no':
                        print('You have entered an invalid command')
                        take_item = input('Would you like to retrieve the item? Type: yes or no\n').lower()
                    if take_item == 'yes':
                        os.system('cls')
                        inventory.append(rooms[current_room]['other']['item'])
                        if len(inventory) >= 7:
                            print(win_game)
                            exit()
                        del rooms[current_room]['other']['item']
                        print('Your inventory:', ', '.join(inventory))
                        print('your location: {}\n'.format(current_room))
                        user_command = input('Please enter a direction or type "exit" to exit the game:\n').title()
                    else:
                        os.system('cls')
                        if len(inventory) == 0:
                            print('Your inventory: [empty]')
                        else:
                            print('Your inventory:', ', '.join(inventory))
                        print('your location: {}\n'.format(current_room))
                        user_command = input('Please enter a direction or type "exit" to exit the game:\n').title()
                else:
                    print()
                    print('{}\n'.format(rooms[current_room]['other']['descript2']))
                    if len(inventory) == 0:
                        print('Your inventory: [empty]\n')
                    else:
                        print('Your inventory:', ', '.join(inventory))
                    take_item = input('Would you like to retrieve the item? Type: yes or no\n').lower()
                    WHILE take_item != 'yes' or 'no':
                        print('You have entered an invalid command')
                        take_item = input('Would you like to retrieve the item? Type: yes or no\n').lower()
                    if take_item == 'yes':
                        os.system('cls')
                        inventory.append(rooms[current_room]['other']['item'])
                        if len(inventory) >= 7:
                            print(win_game)
                            exit()
                        del rooms[current_room]['other']['item']
                        print('Your inventory:', ', '.join(inventory))
                        print('your location: {}\n'.format(current_room))
                        user_command = input('Please enter a direction or type "exit" to exit the game:\n').title()
                    else:
                        os.system('cls')
                        if len(inventory) == 0:
                            print('Your inventory: [empty]')
                        else:
                            print('Your inventory:', ', '.join(inventory))
                        print('your location: {}\n'.format(current_room))
                        user_command = input('Please enter a direction or type "exit" to exit the game:\n').title()
python-3.x
1个回答
0
投票

问题出在

while
循环中的逻辑条件。您应该使用
or
来正确检查这两个条件,而不是使用
and

while take_item != 'yes' and take_item != 'no':
    print('You have entered an invalid command')
    take_item = input('Would you like to retrieve the item? Type: yes or no\n').lower()
© www.soinside.com 2019 - 2024. All rights reserved.