Jordan Pagni
Jordan Pagni

Reputation: 454

Having trouble starting the game in python. The game never starts and the error below shows up

In python I'm trying to create a simple game where the player chooses what cave to go to next, while trying to avoid the wumpus. While setting up the initial cave structure it somehow stops and never starts the game, with the following error:

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

Here's me current code for the game. Any help is appreciated :)

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!")

Upvotes: 0

Views: 112

Answers (2)

clp2
clp2

Reputation: 176

Good detailed answer by TigerhawkT3.

I've recently looked at a version of this code, and you can probably fix one problem with a small change. Fix the typos in the link_caves() function, by indenting the last two lines so that they are inside the 'while' loop:

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)

That change should create the required tunnels, and will call visit_cave() each time through the loop. The last line of visit_cave() removes the correct cave from the unvisited_caves list, which is necessary as TigerhawkT3 mentioned.

You do have some further problems to fix, to finish the initialization and get the game to run.

The traceback statement will show you where to begin looking.

You could add print statements to show the caves list, to help you debug the code before it hangs.

The output might be easier to view while debugging if you change the number of caves to something small like 4-5 instead of 20.

Upvotes: 0

TigerhawkT3
TigerhawkT3

Reputation: 49310

First:

visited_caves = []

Then:

link_caves()

Then:

this_cave = choose_cave(visited_caves)

Then:

cave_number = choice(cave_list)

You are sending an empty list to random.choice(), which fails.

Change link_caves() to only process non-empty lists:

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)

Note that if this function hadn't passed an invalid argument to random.choice(), it would've simply gotten stuck, as it uses a while loop in which the condition is never changed. I notice that you also do this in choose_cave(), with while len(caves) >= 3: cave_number = choice(cave_list). This particular snippet will check the length of caves, and, if it's >= 3, choose a random cave from cave_list. Then it will check the length of caves, find it the same as it used to be, pick a random cave, etc., forever. Perhaps you are expecting random.choice() to remove its selected item from the passed sequence. It does not do that. If you want, you can remove it in a variety of ways, such as doing caves.remove(cave_number) after picking a random cave_number.

Keep in mind that there may be other errors in your code in addition to the one that prompted your question and the others that I've pointed out.

Upvotes: 2

Related Questions