无法在基于文本的游戏中为IT Python类移动房间



''我已经做了大约两周的工作了,我似乎无法弄清楚代码正常工作的逻辑,而且我很难理解如何正确地从字典中提取键或值。请注意,我是一个初学者,有些概念让我难以理解

"下面是我的代码,也是我收到的众多错误之一。如有任何指导,我们将不胜感激

rooms = {
'Camp Crystal Lake Office': {'South': 'Packanack Lodge', 'East': 'Creepy Forest',
'North': 'Main Entrance', 'West': 'Mess Hall'},
'Main Entrance': {'South': 'Camp Crystal Lake Office', 'East': 'Abandoned Shack'},
'Abandoned Shack': {'Jason Voorhees'},
'Mess Hall': {'East': 'Camp Crystal Lake Office'},
'Creepy Forest': {'North': 'Higgins Haven', 'West': 'Camp Crystal Lake Office'},
'Higgins Haven': {'South': 'Creepy Forest'},
'Packanack Lodge': {'East': "Camping Area", 'North': 'Camp Crystal Lake Office'},
'Camping Area': {'West': 'Packanack Lodge'}
}
items_needed = {
'Main Entrance': 'rifle',
'Mess Hall': 'Flash Light',
'Creepy Forest': 'Battery',
'Higgins Haven': 'Car',
'Packanack Lodge': 'Running Sneakers',
'Camping Area': 'Bear Trap'
}
def game_play_rules():
print('Welcome to Camp Crystal Lake!!')
print('You are able to move North, South, East, West, but Jason Voorhees is lurking around.')
print("You will need to collect all 6 items to escape Jason's wrath")
print("If you want to leave the camp immediately, type Exit otherwise enjoy the game!")
# create function to change the location

def get_new_location(location, direction):
new_location = location
for i in rooms:
if i == location:
if direction in rooms[i]:
new_location = rooms[i][direction]
return new_location
game_play_rules()
location = 'Camp Crystal Lake Office'
inventory = []
while True:
print('nYou are currently at the ', location) # shows player where they are starting
if location == 'Abandoned Shack':
print("You walked into Jason's shack, better luck next summer.", end= '')
for i in range(100):
for j in range(1000000):
pass
print('.', end=' ', flush = True)
print()
if len(inventory) == 6:
print("You collected all items and were able to escape - enjoy the rest of your summer")
else:
print("Welp, you failed to collect all the items, game over")
break
direction = input('Which way do you want to go: -->' )
print('You want to go ', (get_new_location(location, direction)))
print(f'nThis location has ',(location, items_needed))
print('Your inventory has ', inventory)
print('nYou already have that in your inventory', inventory)
if direction.lower() == [location][items_needed].lower():
if items_needed[location] not in inventory:
inventory.append(items[location])
continue
direction = direction.capitalize()
if direction == 'Exit':
exit(0)
if direction == 'North' or direction == 'South' or direction == 'East' or direction == 'West':
new_location = get_new_location((location, direction))
if new_location == location:
print('I would not wander to far if I were you.')
else:
location = new_location
else:
print('Wrong way!!')

"错误:欢迎来到水晶湖营地!!你可以向北、向南、向东、向西移动,但Jason Voorhees潜伏在附近。你需要收集所有6件物品才能逃脱杰森的愤怒如果您想立即离开营地,请键入Exit,否则请享受游戏!

您目前在水晶湖营地办公室你想走哪条路:-->南方你想去水晶湖营地办公室追踪(最近一次通话(:文件";C: \Users\jr0sa_000\PycharmProjects\pythonMilestone_v1\Project 2.py",第66行,in如果direction.lower((==[location][items_need].lower((:TypeError:列表索引必须是整数或切片,而不是dict

这个位置有("Cup Crystal Lake办公室",{"主入口":"步枪","餐厅":"闪光灯","Creepy Forest":"炮台","Higgins Haven":"汽车","Packanack Lodge":"跑步运动鞋","露营区":"熊陷阱"}(您的库存有[]

你的库存中已经有了[]#''

new_location = get_new_location((location, direction))

应该是

new_location = get_new_location(location, direction)

还修复了指针不正确的问题。

rooms = {
'Camp Crystal Lake Office': {'South': 'Packanack Lodge', 'East': 'Creepy Forest',
'North': 'Main Entrance', 'West': 'Mess Hall'},
'Main Entrance': {'South': 'Camp Crystal Lake Office', 'East': 'Abandoned Shack'},
'Abandoned Shack': {'Jason Voorhees'},
'Mess Hall': {'East': 'Camp Crystal Lake Office'},
'Creepy Forest': {'North': 'Higgins Haven', 'West': 'Camp Crystal Lake Office'},
'Higgins Haven': {'South': 'Creepy Forest'},
'Packanack Lodge': {'East': "Camping Area", 'North': 'Camp Crystal Lake Office'},
'Camping Area': {'West': 'Packanack Lodge'}
}
items_needed = {
'Main Entrance': 'rifle',
'Mess Hall': 'Flash Light',
'Creepy Forest': 'Battery',
'Higgins Haven': 'Car',
'Packanack Lodge': 'Running Sneakers',
'Camping Area': 'Bear Trap'
}
def game_play_rules():
print('Welcome to Camp Crystal Lake!!')
print('You are able to move North, South, East, West, but Jason Voorhees is lurking around.')
print("You will need to collect all 6 items to escape Jason's wrath")
print("If you want to leave the camp immediately, type Exit otherwise enjoy the game!")
# create function to change the location

def get_new_location(location, direction):
new_location = location
for i in rooms:
if i == location and direction in rooms[i]:
new_location = rooms[i][direction]
return new_location
game_play_rules()
location = 'Camp Crystal Lake Office'
inventory = []
while True:
print('nYou are currently at the ', location) # shows player where they are starting
if location == 'Abandoned Shack':
print("You walked into Jason's shack, better luck next summer.", end= '')
for _ in range(100):
print('.', end=' ', flush = True)
print()
if len(inventory) == 6:
print("You collected all items and were able to escape - enjoy the rest of your summer")
else:
print("Welp, you failed to collect all the items, game over")
break
direction = input('Which way do you want to go: -->' ).lower()
print('You want to go ', (get_new_location(location, direction)))
print(f'nThis location has ',(location, items_needed))
print('Your inventory has ', inventory)
print('nYou already have that in your inventory', inventory)
if direction == rooms[location]:
if items_needed[location] not in inventory:
inventory.append(items_needed[location])
continue
direction = direction.capitalize()
if direction == 'Exit':
exit(0)
if direction in ['North', 'South', 'East', 'West']:
new_location = get_new_location(location, direction)
if new_location == location:
print('I would not wander to far if I were you.')
else:
location = new_location
else:
print('Wrong way!!')

我想你指的是direction == rooms[location]而不是if direction.lower() == [location][items_needed].lower():

rooms = {
'Camp Crystal Lake Office': {'South': 'Packanack Lodge', 'East': 'Creepy Forest',
'North': 'Main Entrance', 'West': 'Mess Hall'},
'Main Entrance': {'South': 'Camp Crystal Lake Office', 'East': 'Abandoned Shack'},
'Abandoned Shack': {'Jason Voorhees'},
'Mess Hall': {'East': 'Camp Crystal Lake Office'},
'Creepy Forest': {'North': 'Higgins Haven', 'West': 'Camp Crystal Lake Office'},
'Higgins Haven': {'South': 'Creepy Forest'},
'Packanack Lodge': {'East': "Camping Area", 'North': 'Camp Crystal Lake Office'},
'Camping Area': {'West': 'Packanack Lodge'}
}
items_needed = {
'Main Entrance': 'rifle',
'Mess Hall': 'Flash Light',
'Creepy Forest': 'Battery',
'Higgins Haven': 'Car',
'Packanack Lodge': 'Running Sneakers',
'Camping Area': 'Bear Trap'
}
def game_play_rules():
print('Welcome to Camp Crystal Lake!!')
print('You are able to move North, South, East, West, but Jason Voorhees is lurking around.')
print("You will need to collect all 6 items to escape Jason's wrath")
print("If you want to leave the camp immediately, type Exit otherwise enjoy the game!")
# create function to change the location

def get_new_location(location, direction):
new_location = location
for i in rooms:
if i == location and direction in rooms[i]:
new_location = rooms[i][direction]
return new_location
game_play_rules()
location = 'Camp Crystal Lake Office'
inventory = []
while True:
print('nYou are currently at the ', location) # shows player where they are starting
if location == 'Abandoned Shack':
print("You walked into Jason's shack, better luck next summer.", end= '')
for _ in range(100):
print('.', end=' ', flush = True)
print()
if len(inventory) == 6:
print("You collected all items and were able to escape - enjoy the rest of your summer")
else:
print("Welp, you failed to collect all the items, game over")
break
direction = input('Which way do you want to go: -->' ).lower()
print('You want to go ', (get_new_location(location, direction)))
print(f'nThis location has ',(location, items_needed))
print('Your inventory has ', inventory)
print('nYou already have that in your inventory', inventory)
if direction == rooms[location]:
if items_needed[location] not in inventory:
inventory.append(items_needed[location])
continue
direction = direction.capitalize()
if direction == 'Exit':
exit(0)
if direction in ['North', 'South', 'East', 'West']:
new_location = get_new_location((location, direction))
if new_location == location:
print('I would not wander to far if I were you.')
else:
location = new_location
else:
print('Wrong way!!')

相关内容

最新更新