我的 if 条件在一个 while 循环内无法打破循环。

huangapple go评论62阅读模式
英文:

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
  • &quot;==&quot; returns True ONLY if it exactly matches.<br>
    For eg: &quot;the Queen&#39;s Chamber&quot; == &quot;Queen&#39;s Chamber&quot; returns False.
  • You were comparing the length of a string to &quot;6&quot; 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 &quot;the&quot; from the names to compare properly
# Use none and not strings players can append the strings with any name
rooms = {&#39;Jail Cell&#39;: {&#39;name&#39;: &#39;Jail Cell&#39;, &#39;South&#39;: &#39;Barracks&#39;,
&#39;text&#39;: &#39;You\&#39;re in the Jail Cell.&#39;, &#39;item&#39;: None},
&#39;Barracks&#39;: {&#39;name&#39;: &#39;Barracks&#39;, &#39;West&#39;: &#39;Dining Hall&#39;, &#39;North&#39;: &#39;Jail Cell&#39;,
&#39;text&#39;: &#39;You\&#39;re in the Barracks.&#39;, &#39;item&#39;: &#39;Strange Potion&#39;},
&#39;Dining Hall&#39;: {&#39;name&#39;: &#39;Dining Hall&#39;, &#39;West&#39;: &#39;Queens Chamber&#39;, &#39;North&#39;: &#39;Alchemist Room&#39;,
&#39;South&#39;: &#39;Bedchamber&#39;, &#39;East&#39;: &#39;Barracks&#39;,
&#39;text&#39;: &#39;You\&#39;re in the Dining hall.&#39;, &#39;item&#39;: &#39;Stained Wine Glass&#39;},
&#39;Queens Chamber&#39;: {&#39;name&#39;: &#39;Queens Chamber&#39;, &#39;East&#39;: &#39;Dining Hall&#39;,
&#39;text&#39;: &#39;You\&#39;re in the Queen\&#39;s Chamber.&#39;, &#39;item&#39;: None},
&#39;Bedchamber&#39;: {&#39;name&#39;: &#39;Bedchamber&#39;, &#39;East&#39;: &#39;Religious Shrine&#39;, &#39;North&#39;: &#39;Dining Hall&#39;,
&#39;text&#39;: &#39;You\&#39;re in the Bedchamber.&#39;, &#39;item&#39;: &#39;Rumor Book&#39;},
&#39;Religious Shrine&#39;: {&#39;name&#39;: &#39;Religious Shrine&#39;, &#39;West&#39;: &#39;Bedchamber&#39;,
&#39;text&#39;: &#39;You\&#39;re in the Religious Shrine.&#39;, &#39;item&#39;: &#39;Dagger Sheath&#39;},
&#39;Alchemist Room&#39;: {&#39;name&#39;: &#39;Alchemist Room&#39;, &#39;East&#39;: &#39;Commanders Chamber&#39;, &#39;South&#39;: &#39;Dining Hall&#39;,
&#39;text&#39;: &#39;You\&#39;re in the Alchemist\&#39;s Room.&#39;, &#39;item&#39;: &#39;Potion Recipe&#39;},
&#39;Commanders Chamber&#39;: {&#39;name&#39;: &#39;Commanders Chamber&#39;, &#39;West&#39;: &#39;Alchemist Room&#39;,
&#39;text&#39;: &#39;You\&#39;re in the Commander\&#39;s Chamber.&#39;, &#39;item&#39;: &#39;Queen\&#39;s Chamber Key&#39;}
}
directions = [&#39;North&#39;, &#39;South&#39;, &#39;East&#39;, &#39;West&#39;]
current_room = rooms[&#39;Jail Cell&#39;]
inventory = []
item =[&#39;Strange Potion&#39;, &#39;Stained Wine Glass&#39;, &#39;Rumor Book&#39;, &#39;Potion Recipe&#39;, &#39;Queen\&#39;s Chamber Key&#39;,&#39;Dagger Sheath&#39;]
# Beginning display
def menu():
print(&#39;The Secret of the Forgotten Queen: A Text Based Adventure&#39;)
print(&#39;To move, type: North, South, East, or West.&#39;)
print(&#39;To search a room, type &quot;Search Room&quot;.&#39;)
print(&#39;To pick up an item, type: &quot;Take&quot; followed by the item name.&#39;)
print(&#39;Collect all 6 items to discover what has happened to the Queen.&#39;)
print(&#39;\nYou awaken in an old Jail Cell. An apparition appears! It\&#39;s a beautiful lady who explains that you must &#39;
&#39;find the secrets of this castle so that the Queen may finally rest.&#39;)
menu()
# Loop for game
while True:
# Players need to know if they are in Queens Chamber 
print(&#39;You\&#39;re in the {}.&#39;.format(current_room[&#39;name&#39;]))
print(&#39;Inventory:&#39;, inventory)
# User input
command = input(&#39;\nWhat\&#39;s your next move Hero?&#39;)
#There was the in name and &quot;==&quot; retruns True ONLY if it exactly matches
if current_room[&#39;name&#39;] == &#39;Queens Chamber&#39;:
print(len(inventory))
if len(inventory) == 6: # Use integer 6
print(&#39;The Queen and her lover the Commander of the Knights. Another unfortunate Romeo and Juliet scenario.&#39;
&#39;What\&#39;s the moral of this story?&#39;)
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(&#39;Nothing found in that direction. Which direction now?&#39;)
elif command == &#39;Take &#39; + (current_room[&#39;item&#39;]):
if (current_room[&#39;item&#39;]) not in inventory:
inventory.append(current_room[&#39;item&#39;])
else:
print(&#39;\nYou have already picked this item up!\n&#39;)
elif command == &#39;Search Room&#39;:
print(&#39;You find a {}.&#39;.format(current_room[&#39;item&#39;]))
# To quit game
elif command == &#39;Quit&#39;:
print(&#39;See ya next time!&#39;)
break
# Invalid command
else:
print(&#39;Invalid input. Review the instructions!&#39;)

huangapple
  • 本文由 发表于 2023年4月11日 12:06:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75982322.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定