Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Hunt the Wumpus game Python help
New Posts  All Forums:Forum Nav:

Hunt the Wumpus game Python help

post #1 of 8
Thread Starter 
Greetings,

I currently am trying to learn python. I obtained the book "Hello! Python" and started reading/doing the books examples. Currently I'm on chapter 2 and I'm a bit stuck.

The book's example is to make the game "Hunt the Wumpus."

The following is the code I'm stuck on:

import random

cave_numbers = range(0,20)
caves = []
for i in cave_numbers:
caves.append([])

unvisited_caves = range(0,20)
visited_caves = [0]
unvisited_caves.remove(0)

while unvisited_caves !=[]:
i = choice(visited_caves)
if len(caves) >= 3:
continue

next_cave = choice(unvisited_caves)
caves
.append(next_cave)
caves[next_cave].append(i)

visited_caves.append(next_cave)
unvisited_caves.remove(next_cave)

for number in cave_numbers:
print number, ":", caves[number]
print '__________'

for i in cave_numbers:
while len(caves) < 3:
passage_to = choice(cave_numbers)
caves
. append(passage_to)

for number in cave_numbers:
print number, ":". caves[number]
print '__________'

When I run the program in cmd, the bolded part pops out an error. It says "Name error: choice is not defined."
I'm guessing that I haven't defined choice before, so it doesn't know what to do with it, but I followed the book's example and it's not working. I tried defining choice as:
choice = visited_caves
but it gives me another error, "Typeerror: 'list' object is not callable.

I have no idea what to try after. Can anyone help?
Vicious Delicious
(14 items)
 
  
CPUMotherboardGraphicsRAM
i7 2700K Stock p8 z77 ITX 660 Ti 2 x 4GB 1600MHz 
Hard DriveMonitorKeyboardPower
180GB SSD, 500GB, 1TB, 1.5TB 2560 x 1440, 1680 x 1050 MX Blues 650W 
CaseAudio
BitFenix Prodigy ODAC > Asgard 2 > DT990 PRO/HE-400 
  hide details  
Reply
Vicious Delicious
(14 items)
 
  
CPUMotherboardGraphicsRAM
i7 2700K Stock p8 z77 ITX 660 Ti 2 x 4GB 1600MHz 
Hard DriveMonitorKeyboardPower
180GB SSD, 500GB, 1TB, 1.5TB 2560 x 1440, 1680 x 1050 MX Blues 650W 
CaseAudio
BitFenix Prodigy ODAC > Asgard 2 > DT990 PRO/HE-400 
  hide details  
Reply
post #2 of 8
Are you sure that there isn't code on the previous page as well? I've made that mistake before smile.gif

But yes, you're right; the variable 'choice' isn't defined when it's referenced (or ever, in this case). To help, the first piece of advice I would give is this:

http://docs.python.org/py3k/tutorial/index.html
http://learnpythonthehardway.org/book/
http://getpython3.com/diveintopython3/

Those are all excellent, free online tutorials. I don't know about the quality of the book that you have, really. Perhaps it's good, perhaps not.

At any rate, choice is going to be a function. I would recheck the book, and read the paragraphs leading and following this code snippet (I would write out what possibly could be the function, but if you haven't been introduced to functions yet, it probably will confuse you more than help!).

Edit: Also, when writing code, use the code tags (For example, {CODE} Code goes here {/CODE}, except replace the curly brackets with square brackets). It's especially important for Python, where indentation is tantamount!
Edited by Conley - 4/24/12 at 6:33pm
post #3 of 8
Thread Starter 
Thanks for the links. Will give them a try. Yea functions are the next paragraph, so no idea on them yet. I'll update as I figure it out.
Vicious Delicious
(14 items)
 
  
CPUMotherboardGraphicsRAM
i7 2700K Stock p8 z77 ITX 660 Ti 2 x 4GB 1600MHz 
Hard DriveMonitorKeyboardPower
180GB SSD, 500GB, 1TB, 1.5TB 2560 x 1440, 1680 x 1050 MX Blues 650W 
CaseAudio
BitFenix Prodigy ODAC > Asgard 2 > DT990 PRO/HE-400 
  hide details  
Reply
Vicious Delicious
(14 items)
 
  
CPUMotherboardGraphicsRAM
i7 2700K Stock p8 z77 ITX 660 Ti 2 x 4GB 1600MHz 
Hard DriveMonitorKeyboardPower
180GB SSD, 500GB, 1TB, 1.5TB 2560 x 1440, 1680 x 1050 MX Blues 650W 
CaseAudio
BitFenix Prodigy ODAC > Asgard 2 > DT990 PRO/HE-400 
  hide details  
Reply
post #4 of 8
Thread Starter 
The book is giving me endless frustrations. I'm going to use your links. Thanks again.
Vicious Delicious
(14 items)
 
  
CPUMotherboardGraphicsRAM
i7 2700K Stock p8 z77 ITX 660 Ti 2 x 4GB 1600MHz 
Hard DriveMonitorKeyboardPower
180GB SSD, 500GB, 1TB, 1.5TB 2560 x 1440, 1680 x 1050 MX Blues 650W 
CaseAudio
BitFenix Prodigy ODAC > Asgard 2 > DT990 PRO/HE-400 
  hide details  
Reply
Vicious Delicious
(14 items)
 
  
CPUMotherboardGraphicsRAM
i7 2700K Stock p8 z77 ITX 660 Ti 2 x 4GB 1600MHz 
Hard DriveMonitorKeyboardPower
180GB SSD, 500GB, 1TB, 1.5TB 2560 x 1440, 1680 x 1050 MX Blues 650W 
CaseAudio
BitFenix Prodigy ODAC > Asgard 2 > DT990 PRO/HE-400 
  hide details  
Reply
post #5 of 8
just came across the same error..
choice is defined in random
so at the top the line should read the same as in the previous examples:

from random import choice

running the program now you dont get any output

go to line 12 and change the while condition:

while unvisited_caves == []


this gives you some output
post #6 of 8
Thread Starter 
I remember changing the import random to from random import choice would clear the first problem, but would create problems of its own. I can't remember what, as I just edited my file to what you have written. However, I have a new problem, line 35.

CMD tells me "AttributeError: 'str' object has no attribute 'caves'
Code:

for number in cave_numbers:
#the next line is line 35   
      print number, ":". caves[number] 
print '__________'


I haven't used the book in a while, am learning through python.org now, but if you have any more suggestions, I would be more than happy to hear it. I would love to make this game work in the near future.
Edited by Chocosuke - 6/27/12 at 1:42pm
Vicious Delicious
(14 items)
 
  
CPUMotherboardGraphicsRAM
i7 2700K Stock p8 z77 ITX 660 Ti 2 x 4GB 1600MHz 
Hard DriveMonitorKeyboardPower
180GB SSD, 500GB, 1TB, 1.5TB 2560 x 1440, 1680 x 1050 MX Blues 650W 
CaseAudio
BitFenix Prodigy ODAC > Asgard 2 > DT990 PRO/HE-400 
  hide details  
Reply
Vicious Delicious
(14 items)
 
  
CPUMotherboardGraphicsRAM
i7 2700K Stock p8 z77 ITX 660 Ti 2 x 4GB 1600MHz 
Hard DriveMonitorKeyboardPower
180GB SSD, 500GB, 1TB, 1.5TB 2560 x 1440, 1680 x 1050 MX Blues 650W 
CaseAudio
BitFenix Prodigy ODAC > Asgard 2 > DT990 PRO/HE-400 
  hide details  
Reply
post #7 of 8
Quote:
Originally Posted by Chocosuke View Post

I remember changing the import random to from random import choice would clear the first problem, but would create problems of its own. I can't remember what, as I just edited my file to what you have written. However, I have a new problem, line 35.
CMD tells me "AttributeError: 'str' object has no attribute 'caves'
Code:
for number in cave_numbers:
#the next line is line 35   
      print number, ":". caves[number] 
print '__________'
I haven't used the book in a while, am learning through python.org now, but if you have any more suggestions, I would be more than happy to hear it. I would love to make this game work in the near future.

I'm fairly new to the Python syntax, but I believe you are recieving that error because you are using a .(period) instead of a ,(comma) on this line:
Code:
print number, ":". caves[number]

should be:
Code:
print number, ":", caves[number]

Not sure if you are using Python 2.7.3 or Python 3.2.3, but if it is 2.7.3 you should give this a read/look over:

http://www.cs.uni.edu/~fienup/cs051f10/

I am currently using this via recommendation from my prof (his own site) because I am switching from Ada 95 to Python 2.7.3 next semister.
Bob's Computer
(16 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 2500K MSI P67A-G45 MSI 6970 Lightning 2GB G Skill Ripjaws X (2x4GB) 1866mhz 9-10-9-28 
Hard DriveOptical DriveCoolingOS
Corsair Force 3 SSD (60GB), X1 WD5000AAKS, X1 W... ASUS DRW-24B1ST/BLK/B/AS Black SATA 24X DVD Burner Noctua NH-C14 Windows 7 64 bit 
MonitorKeyboardPowerCase
Asus VH238H Logitech G510 HALE90 750W NZXT Phantom Black 
MouseMouse PadAudioOther
Logitech G9x SteelSeries 4HD ASUS Xonar DGX / Audio-Technica ATH-AD700 Hauppauge! HVR-1250 TV Tuner 
  hide details  
Reply
Bob's Computer
(16 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 2500K MSI P67A-G45 MSI 6970 Lightning 2GB G Skill Ripjaws X (2x4GB) 1866mhz 9-10-9-28 
Hard DriveOptical DriveCoolingOS
Corsair Force 3 SSD (60GB), X1 WD5000AAKS, X1 W... ASUS DRW-24B1ST/BLK/B/AS Black SATA 24X DVD Burner Noctua NH-C14 Windows 7 64 bit 
MonitorKeyboardPowerCase
Asus VH238H Logitech G510 HALE90 750W NZXT Phantom Black 
MouseMouse PadAudioOther
Logitech G9x SteelSeries 4HD ASUS Xonar DGX / Audio-Technica ATH-AD700 Hauppauge! HVR-1250 TV Tuner 
  hide details  
Reply
post #8 of 8
Thread Starter 
Sorry it took so long to reply, family vacation with pretty much no internet.

I do have the comma instead of a period in another Hunt the Wumpus .py save, and it works better, but more problems heh. Still very new to programming, so I'm giving myself to go over other sites before I tackle the game again.

I'll be sure to check out your site, thanks for the link!
Vicious Delicious
(14 items)
 
  
CPUMotherboardGraphicsRAM
i7 2700K Stock p8 z77 ITX 660 Ti 2 x 4GB 1600MHz 
Hard DriveMonitorKeyboardPower
180GB SSD, 500GB, 1TB, 1.5TB 2560 x 1440, 1680 x 1050 MX Blues 650W 
CaseAudio
BitFenix Prodigy ODAC > Asgard 2 > DT990 PRO/HE-400 
  hide details  
Reply
Vicious Delicious
(14 items)
 
  
CPUMotherboardGraphicsRAM
i7 2700K Stock p8 z77 ITX 660 Ti 2 x 4GB 1600MHz 
Hard DriveMonitorKeyboardPower
180GB SSD, 500GB, 1TB, 1.5TB 2560 x 1440, 1680 x 1050 MX Blues 650W 
CaseAudio
BitFenix Prodigy ODAC > Asgard 2 > DT990 PRO/HE-400 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Hunt the Wumpus game Python help