Python 库存条件,因此我只能添加一次项目并赢得条件
我有两个小问题,首先我需要帮助以获得胜利的条件,在收集了所有的物品后,我应该去抚摸区域并获得输出“你打败了杀人狼,所以我希望你喜欢玩。” 除此之外 我有一个问题,我可以添加相同的项目到我的库存两次,这不应该发生,我应该得到一个输出消息说“你已经有这个项目”。 谁能帮我解决这个问题 这是我目前掌握的代码Python 库存条件,因此我只能添加一次项目并赢得条件,python,Python,我有两个小问题,首先我需要帮助以获得胜利的条件,在收集了所有的物品后,我应该去抚摸区域并获得输出“你打败了杀人狼,所以我希望你喜欢玩。” 除此之外 我有一个问题,我可以添加相同的项目到我的库存两次,这不应该发生,我应该得到一个输出消息说“你已经有这个项目”。 谁能帮我解决这个问题 这是我目前掌握的代码 def game_instructions(): # print a main menu and the commands print("Killer Wolf Text
def game_instructions():
# print a main menu and the commands
print("Killer Wolf Text Game")
print("---" * 20)
print("Collect 6 items to win the game, or be killed by the Wolf.")
print("---" * 20)
print('Move Commands: North , South , East , or West.')
print("---" * 20)
print("Add to Inventory: get 'item name'")
print("---" * 20)
game_instructions()
rooms = {
"Main fair ground": {'South': 'Food stand', 'North': 'Arcade', 'East': 'Corn field', 'West': 'Candy shop'},
"Food stand": {'North': 'Main fair ground', 'East': 'Security', 'item': 'meat'},
"Security": {'West': 'Food stand', 'item': 'armor'},
"Arcade": {'South': 'Main fair ground', 'East': 'Gift shop', 'item': 'sword'},
"Gift shop": {'West': 'Arcade', 'item': 'gloves'},
"Candy shop": {'item': 'candy', 'East': 'Main fair ground'},
"Corn field": {'West': 'Main fair ground', 'North': 'Petting area', 'item': 'repellent'},
"Petting area": {'South': 'Corn field', 'item': 'Killer Wolf'}
}
current_room = 'Main fair ground' # starts player in the Main fair ground
inventory = [] # Adds an inventory
def get_new_room(current_room, direction):
new_room = None
for i in rooms: # starts loop
if i == current_room: # if statement
if direction in rooms[i]: # if statement
new_room = rooms[i][direction] # Assigns new room.
else:
new_room = current_room #this assigns new room to current room if the mentioned direction is not valid
return new_room # returns new room
def get_item(current_room):
if 'item' in rooms[current_room]: # if statement
return rooms[current_room]['item'] # return statement
else:
return 'This room has no item!' # return statement
while (current_room): # gameplay loop
print('You are in the {}'.format(current_room)) # tells player what room they are in.
print('Inventory:', inventory) # shows player their inventory
item = get_item(current_room) # defines item as get item
print('You found the:', item) # tells the player what item they have found
if item == 'Killer Wolf' and len(inventory)<6: # if statement
print('NOM NOM, You did not collected all the necessary items to survive! The game has ended, I hope you '
'enjoyed!') # notifies player game has ended.
break # ends game
direction = input('Enter direction you would like to move. >>') # gets direction from player.
direction = direction.capitalize() # Capitalizes the players input to match what is in the dictionary.
if (direction == 'North' or direction == 'South' or direction == 'East' or direction == 'West'): # if statement
new_room = get_new_room(current_room, direction) # Calling function
if new_room == current_room: # if statement
print('That is a wall not an exit. Try Again!') # Print statement
else:
current_room = new_room # declares current room as new room
elif direction == str('get ' + item).capitalize(): # input statement to add item
if 'item' in inventory: # if statement
print('You have already collected this item. Move to another room!') # print statement
else:
inventory.append(item) # adds item to inventory
else:
print('Not a valid direction!') # Print statement
if len(inventory) == 6: # if statement
print("Congratulations!! You have collected all the necessary items to defeat the killer wolf")
def game_指令():
#打印主菜单和命令
打印(“杀手狼文本游戏”)
打印(“--”*20)
打印(“收集6件物品以赢得游戏,或被狼杀死。”)
打印(“--”*20)
打印('移动命令:北、南、东或西')
打印(“--”*20)
打印(“添加到库存:获取‘项目名称’”)
打印(“--”*20)
游戏说明()
房间={
“主要集市地”:{‘南’:‘食品摊’、‘北’:‘拱廊’、‘东’:‘玉米田’、‘西’:‘糖果店’},
“食品摊”:{'North':'Main fair ground','East':'Security','item':'meat',
“安全”:{'West':'Food stand','item':'armor'},
“拱廊”:{‘南’:‘主会场’、‘东’:‘礼品店’、‘物品’:‘剑’},
“礼品店”:{'West':'Arcade','item':'手套',
“糖果店”:{'item':'Candy','East':'Main fair ground'},
“玉米田”:{'West':'Main fair ground','North':'Petting area','item':'Distribut'},
“宠物区”:{‘南’:‘玉米田’,‘物品’:‘杀手狼’}
}
当前_房间='Main fair ground'#在主游乐场启动玩家
inventory=[]#添加一个库存
def获取新房间(当前房间,方向):
新房间=无
对于房间中的i:#开始循环
if i==当前_房间:#if语句
房间中的if方向[i]:#if语句
新建房间=房间[i][direction]#分配新房间。
其他:
new_room=current_room#如果所述方向无效,则将新房间分配给当前房间
返回新房间#返回新房间
def get_项目(当前_房间):
房间[current_room]中的if“item”:#if语句
归还房间[当前房间]['item']#归还声明
其他:
return“这个房间没有物品!”#返回语句
while(当前游戏室):#游戏循环
print({}格式(当前房间))#告诉玩家他们在哪个房间。
打印(“库存:”,库存)#显示玩家的库存
item=get_item(当前_房间)#将item定义为get item
打印(“您找到:”,物品)#告诉玩家他们找到了什么物品
如果item='Killer Wolf'和len(inventory)下面是所有bug修复的代码:
def game_instructions():
# print a main menu and the commands
print("Killer Wolf Text Game")
print("---" * 20)
print("Collect 6 items to win the game, or be killed by the Wolf.")
print("---" * 20)
print('Move Commands: North , South , East , or West.')
print("---" * 20)
print("Add to Inventory: get 'item name'")
print("---" * 20)
game_instructions()
rooms = {
"Main fair ground": {'South': 'Food stand', 'North': 'Arcade', 'East': 'Corn field', 'West': 'Candy shop'},
"Food stand": {'North': 'Main fair ground', 'East': 'Security', 'item': 'meat'},
"Security": {'West': 'Food stand', 'item': 'armor'},
"Arcade": {'South': 'Main fair ground', 'East': 'Gift shop', 'item': 'sword'},
"Gift shop": {'West': 'Arcade', 'item': 'gloves'},
"Candy shop": {'item': 'candy', 'East': 'Main fair ground'},
"Corn field": {'West': 'Main fair ground', 'North': 'Petting area', 'item': 'repellent'},
"Petting area": {'South': 'Corn field', 'item': 'Killer Wolf'}
}
current_room = 'Main fair ground' # starts player in the Main fair ground
inventory = [] # Adds an inventory
def get_new_room(current_room, direction):
new_room = None
for i in rooms: # starts loop
if i == current_room: # if statement
if direction in rooms[i]: # if statement
new_room = rooms[i][direction] # Assigns new room.
else:
new_room = current_room #this assigns new room to current room if the mentioned direction is not valid
return new_room # returns new room
def get_item(current_room):
if 'item' in rooms[current_room]: # if statement
return rooms[current_room]['item'] # return statement
else:
return 'This room has no item!' # return statement
while (current_room): # gameplay loop
print('You are in the {}'.format(current_room)) # tells player what room they are in.
print('Inventory:', inventory) # shows player their inventory
item = get_item(current_room) # defines item as get item
print('You found the:', item) # tells the player what item they have found
if item == 'Killer Wolf':
if len(inventory)<6:
print('NOM NOM, You did not collected all the necessary items to survive! The game has ended, I hope you '
'enjoyed!') # notifies player game has ended.
break
elif len(inventory)==6:
print("You defeated the Killer Wolf so I hope you enjoyed playing")
break
direction = input('Enter direction you would like to move. >>') # gets direction from player.
direction = direction.capitalize() # Capitalizes the players input to match what is in the dictionary.
if (direction == 'North' or direction == 'South' or direction == 'East' or direction == 'West'): # if statement
new_room = get_new_room(current_room, direction) # Calling function
if new_room == current_room: # if statement
print('That is a wall not an exit. Try Again!') # Print statement
else:
current_room = new_room # declares current room as new room
elif direction == str('get ' + item).capitalize(): # input statement to add item
if item in inventory: # if statement
print('You have already collected this item. Move to another room!') # print statement
else:
inventory.append(item) # adds item to inventory
else:
print('Not a valid direction!') # Print statement
if len(inventory) == 6: # if statement
print("Congratulations!! You have collected all the necessary items to defeat the killer wolf")
def game_指令():
#打印主菜单和命令
打印(“杀手狼文本游戏”)
打印(“--”*20)
打印(“收集6件物品以赢得游戏,或被狼杀死。”)
打印(“--”*20)
打印('移动命令:北、南、东或西')
打印(“--”*20)
打印(“添加到库存:获取‘项目名称’”)
打印(“--”*20)
游戏说明()
房间={
“主要集市地”:{‘南’:‘食品摊’、‘北’:‘拱廊’、‘东’:‘玉米田’、‘西’:‘糖果店’},
“食品摊”:{'North':'Main fair ground','East':'Security','item':'meat',
“安全”:{'West':'Food stand','item':'armor'},
“拱廊”:{‘南’:‘主会场’、‘东’:‘礼品店’、‘物品’:‘剑’},
“礼品店”:{'West':'Arcade','item':'手套',
“糖果店”:{'item':'Candy','East':'Main fair ground'},
“玉米田”:{'West':'Main fair ground','North':'Petting area','item':'Distribut'},
“宠物区”:{‘南’:‘玉米田’,‘物品’:‘杀手狼’}
}
当前_房间='Main fair ground'#在主游乐场启动玩家
inventory=[]#添加一个库存
def获取新房间(当前房间,方向):
新房间=无
对于房间中的i:#开始循环
if i==当前_房间:#if语句
房间中的if方向[i]:#if语句
新建房间=房间[i][direction]#分配新房间。
其他:
new_room=current_room#如果所述方向无效,则将新房间分配给当前房间
返回新房间#返回新房间
def get_项目(当前_房间):
房间[current_room]中的if“item”:#if语句
归还房间[当前房间]['item']#归还声明
其他:
return“这个房间没有物品!”#返回语句
while(当前游戏室):#游戏循环
print({}格式(当前房间))#告诉玩家他们在哪个房间。
打印(“库存:”,库存)#显示玩家的库存
item=get_item(当前_房间)#将item定义为get item
打印(“您找到:”,物品)#告诉玩家他们找到了什么物品
如果项目==‘杀手狼’:
如果你不删除同一个问题并重复10次,你可能会得到答案。