[Tutor] Help
Chris Kavanagh
ckava1 at msn.com
Tue Nov 1 02:02:46 CET 2011
This code is from the book 'Invent your own computer games with Python'
2nd edition. Great book so far. . .
My question comes on the 2nd game (dragon game). Just a simple little
program that lets you choose to enter 'cave one' or 'cave two' by using
the random module, radnom.randint. If you choose the 'wrong' cave, you
get a message saying 'sorry you've been eaten'. Choose the correct cave
and you get a message that 'you get the treasure. Pretty simple. .
However, I'm confused on Line 30 {if chosenCave== str(friendlyCave)}.
Here's the description of this line the author gives:
"Here we check if the integer of the cave we chose ('1' or '2') is equal
to the cave
randomly selected to have the friendly dragon"
My question is, we saved the integer of the cave we chose in the
variable {cave}in line 15, not {chosenCave}. So, what the heck am I
missing?? How is the {chosenCave} variable now holding the choice I made
in the {cave} variable??
Keep in mind I'm a beginner, but geez, I should understand this easily!
I'm very frustrated right now, I should easily know this. But, I don't,
lol. Thanks in advance for any help!! Code below:
dragon.py
This code can be downloaded from http://inventwithpython.com/dragon.py
If you get errors after typing this code in, compare it to the book's
code with the online
diff tool at http://inventwithpython.com/diff or email the author at
al at inventwithpython.com
1. import random
2. import time
3.
4. def displayIntro():
5. print('You are on a planet full of dragons. In front
of you,')
6. print('you see two caves. In one cave, the dragon is
friendly')
7. print('and will share his treasure with you. The
other dragon')
8. print('is greedy and hungry, and will eat you on
sight.')
9. print()
10.
11. def chooseCave():
12. cave = ''
13. while cave != '1' and cave != '2':
14. print('Which cave will you go into? (1 or 2)')
15. cave = input()
16.
17. return cave
18.
19. def checkCave(chosenCave):
20. print('You approach the cave...')
21. time.sleep(2)
22. print('It is dark and spooky...')
23. time.sleep(2)
24. print('A large dragon jumps out in front of you! He
opens his jaws and...')
25. print()
26. time.sleep(2)
27.
28. friendlyCave = random.randint(1, 2)
29.
30. if chosenCave == str(friendlyCave):
31. print('Gives you his treasure!')
32. else:
33. print('Gobbles you down in one bite!')
34.
35. playAgain = 'yes'
36. while playAgain == 'yes' or playAgain == 'y':
37.
38. displayIntro()
58
39.
40. caveNumber = chooseCave()
41.
42. checkCave(caveNumber)
43.
44. print('Do you want to play again? (yes or no)')
More information about the Tutor
mailing list