英文:
My If condition within a while loop doesn't break the loop
问题
以下是您要翻译的代码部分:
rooms = {'Jail Cell': {'name': 'Jail Cell', 'South': 'Barracks',
'text': 'You\'re in the Jail Cell.', 'item': 'whole lot of nothing'},
'Barracks': {'name': 'the Barracks', 'West': 'Dining Hall', 'North': 'Jail Cell',
'text': 'You\'re in the Barracks.', 'item': 'Strange Potion'},
'Dining Hall': {'name': 'the Dining Hall', 'West': 'Queens Chamber', 'North': 'Alchemist Room',
'South': 'Bedchamber', 'East': 'Barracks',
'text': 'You\'re in the Dining hall.', 'item': 'Stained Wine Glass'},
'Queens Chamber': {'name': 'the Queens Chamber', 'East': 'Dining Hall',
'text': 'You\'re in the Queen\'s Chamber.', 'item': 'nothing'},
'Bedchamber': {'name': 'the Bedchamber', 'East': 'Religious Shrine', 'North': 'Dining Hall',
'text': 'You\'re in the Bedchamber.', 'item': 'Rumor Book'},
'Religious Shrine': {'name': 'Religious Shrine', 'West': 'Bedchamber',
'text': 'You\'re in the Religious Shrine.', 'item': 'Dagger Sheath'},
'Alchemist Room': {'name': 'Alchemist Room', 'East': 'Commanders Chamber', 'South': 'Dining Hall',
'text': 'You\'re in the Alchemist\'s Room.', 'item': 'Potion Recipe'},
'Commanders Chamber': {'name': 'Commanders Chamber', 'West': 'Alchemist Room',
'text': 'You\'re in the Commander\'s Chamber.', 'item': 'Queen\'s Chamber Key'}
}
directions = ['North', 'South', 'East', 'West']
current_room = rooms['Jail Cell']
inventory = []
item = ['Strange Potion', 'Stained Wine Glass', 'Rumor Book', 'Potion Recipe', 'Queen\'s Chamber Key']
# Beginning display
def menu():
print('The Secret of the Forgotten Queen: A Text Based Adventure')
print('To move, type: North, South, East, or West.')
print('To search a room, type "Search Room".')
print('To pick up an item, type: "Take" followed by the item name.')
print('Collect all 6 items to discover what has happened to the Queen.')
print('\nYou awaken in an old Jail Cell. An apparition appears! It\'s a beautiful lady who explains that you must '
'find the secrets of this castle so that the Queen may finally rest.')
menu()
# Loop for game
while True:
if current_room['name'] != 'Queens Chamber':
print('You\'re in the {}.'.format(current_room['name']))
print('Inventory:', inventory)
# User input
command = input('\nWhat\'s your next move Hero?')
if current_room['name'] == 'Queens Chamber':
if len(inventory) == '6':
print('The Queen and her lover the Commander of the Knights. Another unfortunate Romeo and Juliet scenario.'
'What\'s the moral of this story?')
break
# The movement
if command in directions:
if command in current_room:
current_room = rooms[current_room[command]]
else:
# No room in that direction
print('Nothing found in that direction. Which direction now?')
elif command == 'Take ' + (current_room['item']):
if (current_room['item']) not in inventory:
inventory.append(current_room['item'])
else:
print('\nYou have already picked this item up!\n')
elif command == 'Search Room':
print('You find a {}.'.format(current_room['item']))
# To quit the game
elif command == 'Quit':
print('See ya next time!')
break
# Invalid command
else:
print('Invalid input. Review the instructions!')
英文:
Struggling to get my code for the final room to finish the text-based game assigned to me. I essentially want the player to be forced to get all 6 items prior to entry. Any help is greatly appreciated. I have tried looking at similar projects from others, can't get that part right. The rest of the game functions appropriately, so this is my last big hill to get over.
How can I have my if condition within a while loop break the loop and end the game?
rooms = {'Jail Cell': {'name': 'Jail Cell', 'South': 'Barracks',
'text': 'You\'re in the Jail Cell.', 'item': 'whole lot of nothing'},
'Barracks': {'name': 'the Barracks', 'West': 'Dining Hall', 'North': 'Jail Cell',
'text': 'You\'re in the Barracks.', 'item': 'Strange Potion'},
'Dining Hall': {'name': 'the Dining Hall', 'West': 'Queens Chamber', 'North': 'Alchemist Room',
'South': 'Bedchamber', 'East': 'Barracks',
'text': 'You\'re in the Dining hall.', 'item': 'Stained Wine Glass'},
'Queens Chamber': {'name': 'the Queens Chamber', 'East': 'Dining Hall',
'text': 'You\'re in the Queen\'s Chamber.', 'item': 'nothing'},
'Bedchamber': {'name': 'the Bedchamber', 'East': 'Religious Shrine', 'North': 'Dining Hall',
'text': 'You\'re in the Bedchamber.', 'item': 'Rumor Book'},
'Religious Shrine': {'name': 'Religious Shrine', 'West': 'Bedchamber',
'text': 'You\'re in the Religious Shrine.', 'item': 'Dagger Sheath'},
'Alchemist Room': {'name': 'Alchemist Room', 'East': 'Commanders Chamber', 'South': 'Dining Hall',
'text': 'You\'re in the Alchemist\'s Room.', 'item': 'Potion Recipe'},
'Commanders Chamber': {'name': 'Commanders Chamber', 'West': 'Alchemist Room',
'text': 'You\'re in the Commander\'s Chamber.', 'item': 'Queen\'s Chamber Key'}
}
directions = ['North', 'South', 'East', 'West']
current_room = rooms['Jail Cell']
inventory = []
item = ['Strange Potion', 'Stained Wine Glass', 'Rumor Book', 'Potion Recipe', 'Queen\'s Chamber Key']
# Beginning display
def menu():
print('The Secret of the Forgotten Queen: A Text Based Adventure')
print('To move, type: North, South, East, or West.')
print('To search a room, type "Search Room".')
print('To pick up an item, type: "Take" followed by the item name.')
print('Collect all 6 items to discover what has happened to the Queen.')
print('\nYou awaken in an old Jail Cell. An apparition appears! It\'s a beautiful lady who explains that you must '
'find the secrets of this castle so that the Queen may finally rest.')
menu()
# Loop for game
while True:
if current_room['name'] != 'Queens Chamber':
print('You\'re in the {}.'.format(current_room['name']))
print('Inventory:', inventory)
# User input
command = input('\nWhat\'s your next move Hero?')
if current_room['name'] == 'Queens Chamber':
if len(inventory) == '6':
print('The Queen and her lover the Commander of the Knights. Another unfortunate Romeo and Juliet scenario.'
'What\'s the moral of this story?')
break
# The movement
if command in directions:
if command in current_room:
current_room = rooms[current_room[command]]
else:
# No room in that direction
print('Nothing found in that direction. Which direction now?')
elif command == 'Take ' + (current_room['item']):
if (current_room['item']) not in inventory:
inventory.append(current_room['item'])
else:
print('\nYou have already picked this item up!\n')
elif command == 'Search Room':
print('You find a {}.'.format(current_room['item']))
# To quit game
elif command == 'Quit':
print('See ya next time!')
break
# Invalid command
else:
print('Invalid input. Review the instructions!')
答案1
得分: 2
以下是翻译好的部分:
- 使用
None
而不是字符串,玩家可以附加字符串的任何名称。我能够添加"nothing"
到我的库存并完成游戏! - 从名称中移除
"the"
以便正确比较。 "=="
仅在完全匹配时返回True
。例如:"the Queen's Chamber" == "Queen's Chamber"
返回False
。- 您正在将字符串的长度与
"6"
字符串进行比较。len
返回整数类型。 - 注意:也可以尝试使用像 VS Code 这样的文本编辑器中的调试器自行调试,编写代码时会遇到许多类似的问题。
此外,代码部分未翻译,如您所要求。
英文:
- Use
None
and not the strings players can append the strings with any name. I was able to add"nothing"
to my inventory and complete the game! - Remove
"the"
from the names to compare properly "=="
returnsTrue
ONLY if it exactly matches.<br>
For eg:"the Queen's Chamber" == "Queen's Chamber"
returnsFalse
.- You were comparing the length of a string to
"6"
a string.len
returns integer type.<br><br>
Note: Try debugging yourself too using debuggers present in text editors like VS Code. You will face many such problems when you code
# Remove "the" from the names to compare properly
# Use none and not strings players can append the strings with any name
rooms = {'Jail Cell': {'name': 'Jail Cell', 'South': 'Barracks',
'text': 'You\'re in the Jail Cell.', 'item': None},
'Barracks': {'name': 'Barracks', 'West': 'Dining Hall', 'North': 'Jail Cell',
'text': 'You\'re in the Barracks.', 'item': 'Strange Potion'},
'Dining Hall': {'name': 'Dining Hall', 'West': 'Queens Chamber', 'North': 'Alchemist Room',
'South': 'Bedchamber', 'East': 'Barracks',
'text': 'You\'re in the Dining hall.', 'item': 'Stained Wine Glass'},
'Queens Chamber': {'name': 'Queens Chamber', 'East': 'Dining Hall',
'text': 'You\'re in the Queen\'s Chamber.', 'item': None},
'Bedchamber': {'name': 'Bedchamber', 'East': 'Religious Shrine', 'North': 'Dining Hall',
'text': 'You\'re in the Bedchamber.', 'item': 'Rumor Book'},
'Religious Shrine': {'name': 'Religious Shrine', 'West': 'Bedchamber',
'text': 'You\'re in the Religious Shrine.', 'item': 'Dagger Sheath'},
'Alchemist Room': {'name': 'Alchemist Room', 'East': 'Commanders Chamber', 'South': 'Dining Hall',
'text': 'You\'re in the Alchemist\'s Room.', 'item': 'Potion Recipe'},
'Commanders Chamber': {'name': 'Commanders Chamber', 'West': 'Alchemist Room',
'text': 'You\'re in the Commander\'s Chamber.', 'item': 'Queen\'s Chamber Key'}
}
directions = ['North', 'South', 'East', 'West']
current_room = rooms['Jail Cell']
inventory = []
item =['Strange Potion', 'Stained Wine Glass', 'Rumor Book', 'Potion Recipe', 'Queen\'s Chamber Key','Dagger Sheath']
# Beginning display
def menu():
print('The Secret of the Forgotten Queen: A Text Based Adventure')
print('To move, type: North, South, East, or West.')
print('To search a room, type "Search Room".')
print('To pick up an item, type: "Take" followed by the item name.')
print('Collect all 6 items to discover what has happened to the Queen.')
print('\nYou awaken in an old Jail Cell. An apparition appears! It\'s a beautiful lady who explains that you must '
'find the secrets of this castle so that the Queen may finally rest.')
menu()
# Loop for game
while True:
# Players need to know if they are in Queens Chamber
print('You\'re in the {}.'.format(current_room['name']))
print('Inventory:', inventory)
# User input
command = input('\nWhat\'s your next move Hero?')
#There was the in name and "==" retruns True ONLY if it exactly matches
if current_room['name'] == 'Queens Chamber':
print(len(inventory))
if len(inventory) == 6: # Use integer 6
print('The Queen and her lover the Commander of the Knights. Another unfortunate Romeo and Juliet scenario.'
'What\'s the moral of this story?')
break
# The movement
if command in directions:
if command in current_room:
current_room = rooms[current_room[command]]
else:
# No room in that direction
print('Nothing found in that direction. Which direction now?')
elif command == 'Take ' + (current_room['item']):
if (current_room['item']) not in inventory:
inventory.append(current_room['item'])
else:
print('\nYou have already picked this item up!\n')
elif command == 'Search Room':
print('You find a {}.'.format(current_room['item']))
# To quit game
elif command == 'Quit':
print('See ya next time!')
break
# Invalid command
else:
print('Invalid input. Review the instructions!')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论