You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

修复游戏exit功能生效后无效输入提示重复出现的问题

游戏命令重复提示无效输入的问题修复

问题描述

游戏原本运行正常,添加exit退出功能后,输入有效移动命令(如go North)时会重复弹出"invalid input"提示,且提示出现在房间信息之前,调整语句位置后问题仍未解决。

错误代码

# declaration
rooms = {
    'Town Square': {'North': 'Bookstore', 'South': 'Restaurant', 'East': 'Jeweler', 'West': 'Tailor', 'item': 'None'},
    'Tailor': {'East': 'Town Square', 'item': 'Suit'},
    'Restaurant': {'North': 'Town Square', 'East': 'Hair Dresser', 'item': 'Salad'},
    'Hair Dresser': {'West': 'Restaurant', 'item': 'Comb'},
    'Bookstore': {'South': 'Town Square', 'East': 'Shoemaker', 'item': 'Bible'},
    'Shoemaker': {'West': 'Bookstore', 'item': 'Shoes'},
    'Jeweler': {'North': 'Chapel', 'West': 'Town Square', 'item': 'Ring'},
    'Chapel': {'South': 'Jeweler', 'item': 'Wife'}
}
state = 'Town Square'


# function
def get_new_state(state, direction):
    new_state = state  # declaring
    for i in rooms:  # loop
        if i == state:  # if
            if direction in rooms[i]:  # if
                new_state = rooms[i][direction]  # assigning new_state
    return new_state  # return


# function
def get_item(state):
    return rooms[state]['item']  # returning Item value


# function
def show_instructions():
    # print a main menu and the commands
    print("Welcome to the Wedding Adventure!")
    print("Collect all 6 items before you reach the chapel, so you will not die of embarrassment when you get left at "
          "the altar")
    print("Move commands: go South, go North, go East, go West")
    print("Add to Inventory: get 'item name'")


show_instructions()  # calling function
inventory = []
while (1):  # gameplay loop
    print('You are in ', state)  # printing state
    print('Inventory:', inventory)  # printing inventory
    item = get_item(state)  # calling get_item function
    print('You see a', item)  # print
    print('------------------------------')
    if item == 'Wife':  # if
        if len(inventory) == 6:
            print('Congratulations! You have collected all the items and got married!')
            print('Thank you for playing the game!')
            exit(0)
        else:
            print('SLAP ... GAME OVER!')
            print('Thanks for playing! Hope you enjoyed it!')
            exit(0)
    direction = input('Enter your move: ')  # asking user
    if direction == 'go East' or direction == 'go West' or direction == 'go North' or direction == 'go South':  # if
        direction = direction[3:]
        new_state = get_new_state(state, direction)  # calling function
        if new_state == state:  # if
            print('The room has wall in that direction enter other direction!')  # print
        else:
            state = new_state  # changing state value to new_state
            print('Invalid input')
    elif direction == str('get ' + item):  # Obtain item
        if item in inventory:  # if item already present in inventory
            print('Item already taken go to another room!!')
        else:
            print(f"{item} retrieved!\n")
            inventory.append(item)  # appending
    if direction == 'exit':
        print('Thank you for playing')
        exit(0)
    else:
        print('Invalid input')

错误输出示例

Enter your move: go North
Invalid input
Invalid input
You are in  Bookstore
Inventory: []
You see a Bible
------------------------------
Enter your move: exit
Thank you for playing

问题原因与修复方案

核心问题

  1. 移动成功分支错误输出无效提示:在移动到有效房间的else分支里,错误添加了print('Invalid input'),完全不符合逻辑,需直接删除。
  2. exit判断独立导致重复提示:exit的判断是独立的if语句而非elif,导致即使前面的命令已经匹配(比如移动命令),只要不是exit,就会执行最后的else块再次打印无效输入。

修正后的完整代码

# declaration
rooms = {
    'Town Square': {'North': 'Bookstore', 'South': 'Restaurant', 'East': 'Jeweler', 'West': 'Tailor', 'item': 'None'},
    'Tailor': {'East': 'Town Square', 'item': 'Suit'},
    'Restaurant': {'North': 'Town Square', 'East': 'Hair Dresser', 'item': 'Salad'},
    'Hair Dresser': {'West': 'Restaurant', 'item': 'Comb'},
    'Bookstore': {'South': 'Town Square', 'East': 'Shoemaker', 'item': 'Bible'},
    'Shoemaker': {'West': 'Bookstore', 'item': 'Shoes'},
    'Jeweler': {'North': 'Chapel', 'West': 'Town Square', 'item': 'Ring'},
    'Chapel': {'South': 'Jeweler', 'item': 'Wife'}
}
state = 'Town Square'


# function
def get_new_state(state, direction):
    new_state = state  # declaring
    for i in rooms:  # loop
        if i == state:  # if
            if direction in rooms[i]:  # if
                new_state = rooms[i][direction]  # assigning new_state
    return new_state  # return


# function
def get_item(state):
    return rooms[state]['item']  # returning Item value


# function
def show_instructions():
    # print a main menu and the commands
    print("Welcome to the Wedding Adventure!")
    print("Collect all 6 items before you reach the chapel, so you will not die of embarrassment when you get left at "
          "the altar")
    print("Move commands: go South, go North, go East, go West")
    print("Add to Inventory: get 'item name'")
    print("Exit game: exit")  # 补充exit命令说明


show_instructions()  # calling function
inventory = []
while True:  # gameplay loop,用True替代(1)更规范
    print('You are in ', state)  # printing state
    print('Inventory:', inventory)  # printing inventory
    item = get_item(state)  # calling get_item function
    print('You see a', item)  # print
    print('------------------------------')
    if item == 'Wife':  # if
        if len(inventory) == 6:
            print('Congratulations! You have collected all the items and got married!')
            print('Thank you for playing the game!')
            exit(0)
        else:
            print('SLAP ... GAME OVER!')
            print('Thanks for playing! Hope you enjoyed it!')
            exit(0)
    direction = input('Enter your move: ')  # asking user
    if direction == 'go East' or direction == 'go West' or direction == 'go North' or direction == 'go South':  # if
        direction = direction[3:]
        new_state = get_new_state(state, direction)  # calling function
        if new_state == state:  # if
            print('The room has wall in that direction enter other direction!')  # print
        else:
            state = new_state  # changing state value to new_state
            # 移除错误的Invalid input打印
    elif direction == f'get {item}':  # 用f-string更简洁,替代str拼接
        if item in inventory:  # if item already present in inventory
            print('Item already taken go to another room!!')
        else:
            print(f"{item} retrieved!\n")
            inventory.append(item)  # appending
    elif direction == 'exit':  # 改为elif,整合到条件分支
        print('Thank you for playing')
        exit(0)
    else:
        print('Invalid input')  # 只有所有有效命令都不匹配时才打印一次

关键修改点

  • 删除了移动成功分支中错误的print('Invalid input')语句
  • 将exit的判断从独立if改为elif,确保只有未匹配任何有效命令时才触发无效输入提示
  • 补充了exit命令的说明到游戏指引中
  • 用True替代while (1),代码更规范
  • 用f-string替代字符串拼接,代码更简洁

内容的提问来源于stack exchange,提问作者John Smith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 10:20:35