在 python 开始游戏时遇到问题。游戏永远不会开始,并出现以下错误
Having trouble starting the game in python. The game never starts and the error below shows up
在 python 中,我正在尝试创建一个简单的游戏,玩家可以在其中选择下一步要去的洞穴,同时尽量避开 wumpus。在设置初始洞穴结构时,它以某种方式停止并且永远不会开始游戏,并出现以下错误:
Traceback (most recent call last):
File "/Users/JPagz95/Documents/Hunt_the_Wumpus_3.py", line 80, in <module>
link_caves()
File "/Users/JPagz95/Documents/Hunt_the_Wumpus_3.py", line 28, in link_caves
this_cave = choose_cave(visited_caves)
File "/Users/JPagz95/Documents/Hunt_the_Wumpus_3.py", line 50, in choose_cave
cave_number = choice(cave_list)
File "/Users/JPagz95/anaconda/lib/python3.5/random.py", line 253, in choice
i = self._randbelow(len(seq))
KeyboardInterrupt
这是我当前的游戏代码。感谢您的帮助:)
from random import choice
cave_numbers = [x for x in range(20)]
unvisited_caves = [x for x in range(20)]
visited_caves = []
def setup_caves(cave_numbers):
""" create a starting list of caves """
caves = []
for number in cave_numbers:
caves.append(number)
return caves
def visit_cave(cave_number):
""" mark a cave as visited """
visited_caves.append(cave_number)
unvisited_caves.remove(cave_number)
def print_caves():
""" print out the current cave structure """
for number in cave_numbers:
print (number, ":", caves[number])
print ('----------')
def link_caves():
""" make sure all of the caves are connected with two way tunnels"""
while unvisited_caves != []:
this_cave = choose_cave(visited_caves)
next_cave = choose_cave(unvisited_caves)
create_tunnel(this_cave, next_cave)
visit_cave(next_cave)
def create_tunnel(cave_from, cave_to):
""" create a tunnel between cave_from and cave_to """
caves[cave_from].append(cave_to)
caves[cave_to].append(cave_from)
def finish_caves():
""" link the rest of the caves with one way tunnels """
for caves in cave_numbers:
while len(caves) < 3:
passage_to = choose_cave(cave_numbers)
caves[cave].append(passage_to)
def choose_cave(cave_list):
""" pick a cave from a list, provided that the
cave has less than 3 tunnels """
cave_number = choice(cave_list)
while len(caves) >= 3:
cave_number = choice(cave_list)
return cave_number
def print_location(player_location):
""" tell the player about where they are """
print ("You are in a cave ", player_location)
print ("From here you can see caves: ")
print (caves[player_location])
if wumpus_location in caves[player_location]:
print ("I smell a wumpus!")
def get_next_location():
""" Get the player's next location """
print ("Which cave next?")
player_input = input(">")
if (not player_input.isdigit() or
int(player_input) not in
caves[player_location]):
print (player_input + "?")
print ("That's not a direction I can see!")
return none
else:
return int(player_input)
caves = setup_caves(cave_numbers)
visit_cave(0)
print_caves()
link_caves()
print_caves()
finish_caves()
wumpus_location = choice(cave_numbers)
player_location = choice(cave_numbers)
while player_location == wumpus_location:
player_location = choice(cave_numbers)
print ("Welcome to Hunt the Wumpus!")
print ("You can see ", len(cave_numbers), " caves")
print ("To play, just type the number")
print ("of the cave you wish to enter next")
while True:
print_location(player_location)
new_location = get_next_location()
if new_location != None:
player_location = new_location
if player_location == wumpus_location:
print ("Aargh! You got eaten by a wumpus!")
第一个:
visited_caves = []
然后:
link_caves()
然后:
this_cave = choose_cave(visited_caves)
然后:
cave_number = choice(cave_list)
您正在向 random.choice()
发送一个空的 list
,但失败了。
将 link_caves()
更改为仅处理 non-empty list
s:
def link_caves():
""" make sure all of the caves are connected with two way tunnels"""
if not (visited_caves and unvisited_caves):
return
this_cave = choose_cave(visited_caves)
next_cave = choose_cave(unvisited_caves)
create_tunnel(this_cave, next_cave)
visit_cave(next_cave)
请注意,如果此函数没有向 random.choice()
传递无效参数,它就会卡住,因为它使用了条件永远不会改变的 while
循环。我注意到您也在 choose_cave()
和 while len(caves) >= 3: cave_number = choice(cave_list)
中执行此操作。这个特定的片段将检查 caves
的长度,如果是 >= 3
,则从 cave_list
中选择一个随机洞穴。然后它会检查 caves
的长度,找到它和以前一样,选择一个随机的洞穴等等,永远。也许您希望 random.choice()
从传递的序列中删除其选定的项目。它不会那样做。如果需要,您可以通过多种方式将其删除,例如在随机选择 cave_number
.
后执行 caves.remove(cave_number)
请记住,除了提示您问题的错误和我已经指出的其他错误之外,您的代码中可能还有其他错误。
TigerhawkT3 的详细回答。
我最近查看了此代码的一个版本,您可能可以通过一个小的更改来解决一个问题。修复 link_caves() 函数中的拼写错误,方法是缩进最后两行,使它们位于 'while' 循环内:
def link_caves():
""" make sure all of the caves are connected with two way tunnels"""
while unvisited_caves != []:
this_cave = choose_cave(visited_caves)
next_cave = choose_cave(unvisited_caves)
create_tunnel(this_cave, next_cave)
visit_cave(next_cave)
该更改应创建所需的隧道,并且每次循环都会调用 visit_cave()。
visit_cave() 的最后一行从 unvisited_caves 列表中删除了正确的洞穴,正如 TigerhawkT3 提到的那样,这是必要的。
您确实还有一些问题需要解决,以完成初始化并让游戏进入 运行。
追溯语句将告诉您从哪里开始查找。
您可以添加打印语句来显示洞穴列表,以帮助您在代码挂起之前进行调试。
如果将洞穴数量从 20 改为 4-5 之类的小值,调试时输出可能更易于查看。
在 python 中,我正在尝试创建一个简单的游戏,玩家可以在其中选择下一步要去的洞穴,同时尽量避开 wumpus。在设置初始洞穴结构时,它以某种方式停止并且永远不会开始游戏,并出现以下错误:
Traceback (most recent call last):
File "/Users/JPagz95/Documents/Hunt_the_Wumpus_3.py", line 80, in <module>
link_caves()
File "/Users/JPagz95/Documents/Hunt_the_Wumpus_3.py", line 28, in link_caves
this_cave = choose_cave(visited_caves)
File "/Users/JPagz95/Documents/Hunt_the_Wumpus_3.py", line 50, in choose_cave
cave_number = choice(cave_list)
File "/Users/JPagz95/anaconda/lib/python3.5/random.py", line 253, in choice
i = self._randbelow(len(seq))
KeyboardInterrupt
这是我当前的游戏代码。感谢您的帮助:)
from random import choice
cave_numbers = [x for x in range(20)]
unvisited_caves = [x for x in range(20)]
visited_caves = []
def setup_caves(cave_numbers):
""" create a starting list of caves """
caves = []
for number in cave_numbers:
caves.append(number)
return caves
def visit_cave(cave_number):
""" mark a cave as visited """
visited_caves.append(cave_number)
unvisited_caves.remove(cave_number)
def print_caves():
""" print out the current cave structure """
for number in cave_numbers:
print (number, ":", caves[number])
print ('----------')
def link_caves():
""" make sure all of the caves are connected with two way tunnels"""
while unvisited_caves != []:
this_cave = choose_cave(visited_caves)
next_cave = choose_cave(unvisited_caves)
create_tunnel(this_cave, next_cave)
visit_cave(next_cave)
def create_tunnel(cave_from, cave_to):
""" create a tunnel between cave_from and cave_to """
caves[cave_from].append(cave_to)
caves[cave_to].append(cave_from)
def finish_caves():
""" link the rest of the caves with one way tunnels """
for caves in cave_numbers:
while len(caves) < 3:
passage_to = choose_cave(cave_numbers)
caves[cave].append(passage_to)
def choose_cave(cave_list):
""" pick a cave from a list, provided that the
cave has less than 3 tunnels """
cave_number = choice(cave_list)
while len(caves) >= 3:
cave_number = choice(cave_list)
return cave_number
def print_location(player_location):
""" tell the player about where they are """
print ("You are in a cave ", player_location)
print ("From here you can see caves: ")
print (caves[player_location])
if wumpus_location in caves[player_location]:
print ("I smell a wumpus!")
def get_next_location():
""" Get the player's next location """
print ("Which cave next?")
player_input = input(">")
if (not player_input.isdigit() or
int(player_input) not in
caves[player_location]):
print (player_input + "?")
print ("That's not a direction I can see!")
return none
else:
return int(player_input)
caves = setup_caves(cave_numbers)
visit_cave(0)
print_caves()
link_caves()
print_caves()
finish_caves()
wumpus_location = choice(cave_numbers)
player_location = choice(cave_numbers)
while player_location == wumpus_location:
player_location = choice(cave_numbers)
print ("Welcome to Hunt the Wumpus!")
print ("You can see ", len(cave_numbers), " caves")
print ("To play, just type the number")
print ("of the cave you wish to enter next")
while True:
print_location(player_location)
new_location = get_next_location()
if new_location != None:
player_location = new_location
if player_location == wumpus_location:
print ("Aargh! You got eaten by a wumpus!")
第一个:
visited_caves = []
然后:
link_caves()
然后:
this_cave = choose_cave(visited_caves)
然后:
cave_number = choice(cave_list)
您正在向 random.choice()
发送一个空的 list
,但失败了。
将 link_caves()
更改为仅处理 non-empty list
s:
def link_caves():
""" make sure all of the caves are connected with two way tunnels"""
if not (visited_caves and unvisited_caves):
return
this_cave = choose_cave(visited_caves)
next_cave = choose_cave(unvisited_caves)
create_tunnel(this_cave, next_cave)
visit_cave(next_cave)
请注意,如果此函数没有向 random.choice()
传递无效参数,它就会卡住,因为它使用了条件永远不会改变的 while
循环。我注意到您也在 choose_cave()
和 while len(caves) >= 3: cave_number = choice(cave_list)
中执行此操作。这个特定的片段将检查 caves
的长度,如果是 >= 3
,则从 cave_list
中选择一个随机洞穴。然后它会检查 caves
的长度,找到它和以前一样,选择一个随机的洞穴等等,永远。也许您希望 random.choice()
从传递的序列中删除其选定的项目。它不会那样做。如果需要,您可以通过多种方式将其删除,例如在随机选择 cave_number
.
caves.remove(cave_number)
请记住,除了提示您问题的错误和我已经指出的其他错误之外,您的代码中可能还有其他错误。
TigerhawkT3 的详细回答。
我最近查看了此代码的一个版本,您可能可以通过一个小的更改来解决一个问题。修复 link_caves() 函数中的拼写错误,方法是缩进最后两行,使它们位于 'while' 循环内:
def link_caves():
""" make sure all of the caves are connected with two way tunnels"""
while unvisited_caves != []:
this_cave = choose_cave(visited_caves)
next_cave = choose_cave(unvisited_caves)
create_tunnel(this_cave, next_cave)
visit_cave(next_cave)
该更改应创建所需的隧道,并且每次循环都会调用 visit_cave()。 visit_cave() 的最后一行从 unvisited_caves 列表中删除了正确的洞穴,正如 TigerhawkT3 提到的那样,这是必要的。
您确实还有一些问题需要解决,以完成初始化并让游戏进入 运行。
追溯语句将告诉您从哪里开始查找。
您可以添加打印语句来显示洞穴列表,以帮助您在代码挂起之前进行调试。
如果将洞穴数量从 20 改为 4-5 之类的小值,调试时输出可能更易于查看。