[英]How do I get the words to print as whole words?
我正在使用下面的代碼完成我的第一個 Python 編程課程的最終項目。 其中大部分已經有效。 這個想法是一個基於文本的冒險游戲,你需要在到達“老板室”之前收集 6 件物品。 唯一給我帶來問題的是狀態功能,它旨在打印玩家庫存。 然而,當玩家去收集第一件物品時,它不會打印物品欄:[自畫像],而是物品欄:['S', 'e', 'l', 'f', ' ', 'P', 'o'、'r'、't'、'r'、'a'、'i'、't']。 不知道如何解決這個問題,盡管這似乎是一個愚蠢的問題,哈哈,但我是初學者! 我的完整代碼如下。
def instructions():
print('You are taking part in a museum heist!')
print("Collect all 6 paintings before you find your mafia boss, or you will get fired!")
print("To move, type: North, South, East, or West.")
print("To collect an item, type: Collect")
print('To quit, type Quit')
instructions()
print('----------------------')
def status():
print('You are in the', current_room, 'Room')
print('Inventory:', str(inventory))
if 'item' in rooms[current_room]:
print('You see an artwork called', str(rooms[current_room]['item']))
print('----------------------')
inventory = []#placeholders
item = ' '
def main():
inventory = []
rooms = {
'Foyer': {'East': 'Contemporary'},
'Contemporary': {'North': 'Impressionist', 'East': 'Abstract', 'West': 'Foyer', 'South': 'Surrealist', 'item': 'Self Portrait'},
'Abstract': {'North': 'Pop Art', 'West': 'Contemporary', 'item': 'Convergence'},
'Pop Art': {'South': 'Abstract', 'item': 'Shot Marilyns'},
'Surrealist': {'North': 'Contemporary', 'East': 'Realist', 'item': 'The Persistence of Memory'},
'Impressionist': {'South': 'Contemporary', 'East': 'Expressionist', 'item': 'Poppies'},
'Expressionist': {'West': 'Impressionist', 'item': 'Starry Night'},
'Realist': {'West': 'Surrealist', 'item': 'Mafia Boss'}
}
current_room = 'Foyer'
while True:
status()
if rooms[current_room] == rooms['Realist']:
print('Oh no! Your boss is angry you did not steal all the paintings! You have been fired. GAME OVER.')
break
if len(inventory) == 6:
print('Congratulations! You got all the paintings. You Win!!')
break
print('----------------------')
print('Which way?')
move = input()
if move == 'Quit':
print('Sorry to see you go!')
break
elif move == 'Collect':
if 'item' in rooms[current_room]:
inventory += str(rooms[current_room]['item'])
print("You collected", str(rooms[current_room]['item']))
del rooms[current_room]['item']
else:
print('There is no item here.')
elif move == 'North' or 'South' or 'East' or 'West':
if move in rooms[current_room]:
current_room = rooms[current_room][move]
else:
print("Sorry, you can't go that way.")
move = input()
current_room = current_room
else:
print('Invalid move!')
首先,您可以使用print()
查看不同時刻變量中的內容 - 這樣您就可以看到在哪個時刻創建['S', 'e', 'l', 'f', ' ', 'P', 'o', 'r', 't', 'r', 'a', 'i', 't']
您添加新項目使用
inventory += ...
但這是捷徑
inventory.extend( other_list )
它期望列表作為參數,但您有字符串Self Portrait
,Python 可以將其視為字符列表。
你應該使用.append()
inventory.append( item )
或者您應該將列表與item
inventory.extend( [item] )
inventory += [item]
我看到其他問題。
它應該是
elif move == 'North' or move == 'South' or move == 'East' or move == 'West':
或者
elif move in ('North', 'South', 'East', 'West'):
因為我不喜歡寫長的單詞並且使用shift
所以我會這樣做
if move.lower() in ('quit', 'q'):
#...code...
elif move.lower() in ('collect', 'c'):
#...code...
現在玩家可以使用Quit
、 quit
、 QUIT
、 QuIt
等,或者簡單地使用q
或Q
。 Collect
。
我會對'North', 'South', 'East', 'West'
做同樣的事情'North', 'South', 'East', 'West'
但你在字典rooms
使用它,所以我會使用其他字典將短e
轉換為長East
等。
shortcuts = {
'n': 'North',
's': 'South',
'e': 'East',
'w': 'West',
}
while True:
# ... code ...
move = input()
move = shortcuts.get(move.lower(), move)
# ... code ...
如果它在快捷方式中找到move.lower()
- 即。 e
- 然后它得到全名 - 即。 East
。 但是它沒有在shortcuts
找到較低的文本,然后它保持原始值不被move
( get()
第二個參數)
完整的工作代碼。
為了使代碼更具可讀性,我將所有函數放在開頭。
# --- functions ---
def instructions():
print('You are taking part in a museum heist!')
print("Collect all 6 paintings before you find your mafia boss, or you will get fired!")
print("To move, type: North, South, East, or West.")
print("To collect an item, type: Collect")
print('To quit, type Quit')
def status():
print('You are in the', current_room, 'Room')
#print('Inventory:', inventory)
print('Inventory:', ', '.join(inventory))
if 'item' in rooms[current_room]:
print('You see an artwork called', rooms[current_room]['item'])
def main():
global inventory
inventory = []
# --- main ---
# - variables -
inventory = []#placeholders
item = ' '
rooms = {
'Foyer': {'East': 'Contemporary'},
'Contemporary': {'North': 'Impressionist', 'East': 'Abstract', 'West': 'Foyer', 'South': 'Surrealist', 'item': 'Self Portrait'},
'Abstract': {'North': 'Pop Art', 'West': 'Contemporary', 'item': 'Convergence'},
'Pop Art': {'South': 'Abstract', 'item': 'Shot Marilyns'},
'Surrealist': {'North': 'Contemporary', 'East': 'Realist', 'item': 'The Persistence of Memory'},
'Impressionist': {'South': 'Contemporary', 'East': 'Expressionist', 'item': 'Poppies'},
'Expressionist': {'West': 'Impressionist', 'item': 'Starry Night'},
'Realist': {'West': 'Surrealist', 'item': 'Mafia Boss'}
}
shortcuts = {
'n': 'North',
's': 'South',
'e': 'East',
'w': 'West',
}
current_room = 'Foyer'
# - code -
instructions()
print('----------------------')
while True:
status()
if rooms[current_room] == rooms['Realist']:
print('Oh no! Your boss is angry you did not steal all the paintings! You have been fired. GAME OVER.')
break
if len(inventory) == 6:
print('Congratulations! You got all the paintings. You Win!!')
break
print('----------------------')
print('Which way?')
move = input()
move = shortcuts.get(move.lower(), move)
print('[DEBUG] move:', move)
if move.lower() in ('quit', 'q'):
print('Sorry to see you go!')
break
elif move.lower() in ('collect', 'c'):
if 'item' in rooms[current_room]:
inventory.append(rooms[current_room]['item'])
print("You collected", rooms[current_room]['item'])
del rooms[current_room]['item']
else:
print('There is no item here.')
elif move.lower() in ('north', 'south', 'east', 'west'):
if move in rooms[current_room]:
current_room = rooms[current_room][move]
else:
print("Sorry, you can't go that way.")
#move = input()
#current_room = current_room
else:
print('Invalid move!')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.