controlpanel
traceytexthighlight #DDDDFF
view.list.DragonSourceCode.font-family: Courier, monospace; position: absolute; top: 0px; left: 0px; height: 96%; width: 56%; overflow: auto; padding: 4px;
1, 2, 4, 11, 19, 35, 36, 38, 4, 5, 6, 7, 8, 9, 40, 11, 12, 13, 14, 15, 13, 14, 15, 13, 17, 42, 19, 20, 21, 22, 23, 24, 25, 26, 28, 30, 31, 44, 45, 36, 38, 4, 5, 6, 7, 8, 9, 40, 11, 12, 13, 14, 15, 13, 17, 42, 19, 20, 21, 22, 23, 24, 25, 26, 28, 30, 33, 44, 45, 36, 46
Source Code
- import random
- import time
- def displayIntro():
- print 'You are on a planet full of dragons. In front of you,'
- print 'you see two caves. In one cave, the dragon is friendly'
- print 'and will share his treasure with you. The other dragon'
- print 'is greedy and hungry, and will eat you on sight.'
- print
- def chooseCave():
- cave = ''
- while cave != '1' and cave != '2':
- print 'Which cave will you go into? (1 or 2)'
- cave = raw_input()
- return cave
- def checkCave(chosenCave):
- print 'You approach the cave...'
- time.sleep(2)
- print 'It is dark and spooky...'
- time.sleep(2)
- print 'A large dragon jumps out in front of you! He opens his jaws and...'
- print
- time.sleep(2)
- friendlyCave = random.randint(1, 2)
- if chosenCave == str(friendlyCave):
- print 'Gives you his treasure!'
- else:
- print 'Gobbles you down in one bite!'
- playAgain = 'yes'
- while playAgain == 'yes' or playAgain == 'y':
- displayIntro()
- caveNumber = chooseCave()
- checkCave(caveNumber)
- print 'Do you want to play again? (yes or no)'
- playAgain = raw_input()
view.multispan.DragonVariables.position: absolute; top: 0px; left: 60%; height: 32%; width: 36%; overflow: auto; padding: 4px;
1.Variables
Global Scope
Local Scope
7.Variables
Global Scope
playAgain == 'yes'
Local Scope
18.Variables
Global Scope
playAgain == 'yes'
Local Scope
cave == ''
21.Variables
Global Scope
playAgain == 'yes'
Local Scope
cave == '3'
24.Variables
Global Scope
playAgain == 'yes'
Local Scope
cave == '2'
26.Variables
Global Scope
playAgain == 'yes'
caveNumber == '2'
Local Scope
28.Variables
Global Scope
playAgain == 'yes'
caveNumber == '2'
Local Scope
chosenCave == '2'
36.Variables
Global Scope
playAgain == 'yes'
caveNumber == '2'
Local Scope
chosenCave == '2'
friendlyCave == 2
38.Variables
Global Scope
playAgain == 'yes'
caveNumber == '2'
Local Scope
40.Variables
Global Scope
playAgain == 'y'
caveNumber == '2'
Local Scope
51.Variables
Global Scope
playAgain == 'y'
caveNumber == '2'
Local Scope
cave == ''
54.Variables
Global Scope
playAgain == 'y'
caveNumber == '2'
Local Scope
cave == '1'
56.Variables
Global Scope
playAgain == 'y'
caveNumber == '1'
Local Scope
58.Variables
Global Scope
playAgain == 'y'
caveNumber == '1'
Local Scope
chosenCave == '1'
66.Variables
Global Scope
playAgain == 'y'
caveNumber == '1'
Local Scope
chosenCave == '1'
friendlyCave == 2
68.Variables
Global Scope
playAgain == 'y'
caveNumber == '1'
Local Scope
70.Variables
Global Scope
playAgain == 'n'
caveNumber == '1'
Local Scope
view.simple.DragonNotes.position: absolute; top: 33%; left: 60%; height: 32%; width: 36%; overflow: auto; padding: 4px;
default.Notes
1.Notes
This imports the random module, which has the randint() function we will use.
2.Notes
This imports the time module, which has the sleep() function we will use.
3.Notes
This defines the displayIntro() function. Execution will skip past this function, because we are only defining the function, not calling it.
4.Notes
This defines the chooseCave() function. Again, we are only defining the function, not calling it.
5.Notes
This defines the checkCave() function. checkCave() is defined with one parameter, chosenCave. Parameters are the variables that go in between the parentheses in a function definition.
6.Notes
Here we set the playAgain variable to 'yes'. We do this because our game will keep playing in a loop as long as playAgain is set to 'yes' or 'y'.
7.Notes
This while loop's condition checks to see if playAgain has the 'yes' string or the 'y' string stored in it.
8.Notes
The first thing we do inside the loop is call the displayIntro() function. This will move execution to line 4.
9.Notes
Here we are at the start of the displayIntro() function.
10.Notes
We now print the game's introduction to the player.
11.Notes
We now print the game's introduction to the player.
12.Notes
We now print the game's introduction to the player.
13.Notes
We now print the game's introduction to the player.
14.Notes
We print a blank line.
15.Notes
Since the displayIntro() function ended, the program's execution moves to the next line after the original function call. On this line, we call chooseCave() and then assign the function call's return value to the caveNumber variable.
16.Notes
This is the start of the chooseCave() function.
17.Notes
cave is first assigned the blank string. Remember that because we are inside a function, new variables will be in the local scope.
18.Notes
This loop will continue to loop until the cave variable is equal to either '1' or '2'
19.Notes
We ask the player which cave they want to enter.
20.Notes
Let's pretend the player entered 3 as their answer.
21.Notes
Since the cave variable is equal to '3', both parts of the condition are True, so the loop's condition is True and we enter the loop again.
22.Notes
We ask the player to enter a cave number again.
23.Notes
Let's pretend the player entered 2.
24.Notes
Because cave is not equal to '1' but is equal to '2', this expression becomes True and False, which evaluates to False. (The and operator requires both sides to be True in order to evalute to True).
25.Notes
We skip past the loop, and for the return value of this function call, we return the string '2'.
26.Notes
When we leave a function, the variables in the function's local scope are forgotten and destroyed. Now we call the checkCave() function.
27.Notes
chosenCave is a parameter for this function, and will be assigned the value in caveNumber (which was the argument when this function was called).
29.Notes
This call to the time.sleep() function will cause the program to pause for 2 seconds.
35.Notes
We make a call to random.randint() to have it randomly return a 1 or a 2, which is stored in the local variable, friendlyCave. Let's pretend the value randomly selected was 2.
36.Notes
The return value of the str(friendlyCave) call was the string '2', which is equal to the stirng stored in chosenCave. That makes the condition evaluate to True.
37.Notes
The player has won the treasure!
38.Notes
Again, now that the execution has moved out of the function, all the local variables are destroyed. Now we ask the player if she wants to play again.
39.Notes
Let's pretend the player typed in y.
40.Notes
The right side of the or operator evaluates to True, which makes the entire condition evaluate to True. We enter the while loop again.
53.Notes
Let's pretend the player enters 1.
56.Notes
The value of caveNumber was changed to the return value of the function call to chooseCave() (which was '1'). Now we call checkCave(), passing caveNumber's value as the argument.
65.Notes
This time, let's pretend the random integer returned by randint() was 1.
66.Notes
This condition evaluates to False.
67.Notes
Because the if statement's condition was False, we will execute the code in the else-block. The player has been eaten.
69.Notes
Let's say the player is tired of playing this game, and she enters n.
70.Notes
Since neither side of the or operator evaluates the True, the entire condition evaluates to False. So we skip past the while-block.
71.Notes
There are no more lines of code to execute, so the program terminates.
view.appendscroll.DragonOutput.position: absolute; top: 66%; left: 60%; height: 31%; width: 36%; overflow: auto; padding: 4px; color: 0000FF;
1.Output
11.You are on a planet full of dragons. In front of you,
12.you see two caves. In one cave, the dragon is friendly
13.and will share his treasure with you. The other dragon
14.is greedy and hungry, and will eat you on sight.
15.
20.Which cave will you go into? (1 or 2)
21.3
23.Which cave will you go into? (1 or 2)
24.2
29.You approach the cave...
31.It is dark and spooky...
33.A large dragon jumps out in front of you! He opens his jaws and...
34.
38.Gives you his treasure!
39.Do you want to play again? (yes or no)
40.y
44.You are on a planet full of dragons. In front of you,
45.you see two caves. In one cave, the dragon is friendly
46.and will share his treasure with you. The other dragon
47.is greedy and hungry, and will eat you on sight.
48.
53.Which cave will you go into? (1 or 2)
54.1
59.You approach the cave...
61.It is dark and spooky...
63.A large dragon jumps out in front of you! He opens his jaws and...
64.
68.Gobbles you down in one bite!
69.Do you want to play again? (yes or no)
70.n