Python文本类游戏输入map后输出None的问题求助
问题解决:文本游戏输入"map"时额外输出"None"
问题说明
在脚本课程的学校项目中制作的文本类游戏,代码测试基本正常,但输入"map"指令显示房间布局时,会额外输出"None"。
原因分析
问题出在map命令的处理逻辑中:
elif command == "map": print(map()) continue
map()函数内部已经通过print()语句输出了地图内容,且该函数没有定义返回值(Python中无返回值的函数默认返回None)。当使用print(map())时,会先执行map()打印地图,随后打印map()的返回值None,导致出现额外的输出。
解决方法
将print(map())改为直接调用map()函数即可,不需要用print()包裹:
elif command == "map": map() continue
修改后的完整代码
def intro(): print('Welcome to the Hungry Karen game!!!') print('Karen is Hungry, she ordered food for pickup. However she forgot to complete the online order.') print('She is very upset with the restaurant that her order is not ready, she wants answers!') print('Collect your items before dealing with her or she will ruin your day.') print('You will need: \n - A cigarette in the Outside Alley \n - Pencil from the Office \n - A beer from the Walk-in Cooler \n - Cupcake from the Kitchen \n - Your notepad from the Dining Room \n - An espresso from the Barista Parlor') print('You wont need any supplies from the bathroom, but feel free to take a few minutes to yourself here.') print('See a floorplan of the restaurant by typing "map"') intro() rooms = { 'Host Stand': {'west': 'Barista Parlor'}, 'Barista Parlor': {'east': 'Host Stand', 'south': 'Dining Room', 'item': 'espresso'}, 'Dining Room': {'west': 'Bathroom', 'north': 'Barista Parlor', 'east': 'Office', 'south': 'Kitchen', 'item': 'notepad'}, 'Bathroom': {'east': 'Dining Room'}, 'Kitchen': {'east': 'Walk-in Cooler', 'north': 'Dining Room', 'item': 'cupcake'}, 'Office': {'north': 'Outside Alley', 'west': 'Dining Room', 'item': 'pencil'}, 'Outside Alley': {'south': 'Office', 'item': 'cigarette'}, 'Walk-in Cooler': {'west': 'Kitchen', 'item': 'beer'} } # Player begins game in the Bathroom starting_room = 'Bathroom' # setting room up current_room = starting_room # define inventory dictionary inventory = [] # Function to display player's status and possible commands def display_status(): print(' ') print('You are in the', current_room) print('Inventory:', inventory) if 'item' in rooms[current_room]: print('You see a', rooms[current_room]['item']) print("\nEnter your move:") def map(): print( '\n Barista Parlor ----- Host Stand' '\n | ' '\n | Outside Alley ' '\n | | ' '\n Bathroom - Dining Room ----Office' '\n |' '\n Kitchen -- Walk-in Cooler') while True: # Show current status display_status() # Get the player's next move move = input('>').strip().split() if len(move) < 1: print('Invalid input. Try Again') command = move[0].lower() if command == "go": if len(move) < 2: print("Invalid input. Try again.") continue direction = move[1] if direction in rooms[current_room]: current_room = rooms[current_room][direction] else: print("You can't go that way!") elif command == "get": if len(move) < 2: print("Invalid input. Try again.") continue item = " ".join(move[1:]) if 'item' in rooms[current_room] and rooms[current_room]['item'] == item: inventory.append(item) del rooms[current_room]['item'] print(f"you got the {item}") else: print("Can't get that item!") elif command == "quit": print('Thanks for playing!') break # Map print out feature !!!!!!!!!!! elif command == "map": map() continue else: print("Invalid command. Try again.") # Check win/lose conditions if current_room == 'Host Stand': if len(inventory) == 6: print('You kept your cool while Karen expressed how upset this made her. She left happy!') print('You even got tipped extra on top! Good Job!') break else: print("Karen made you cry and rethink your career choices. Game Over. You lose") break
内容的提问来源于stack exchange,提问作者Joshua Mantooth
相关产品推荐
相关产品推荐

