Source Code

  1. import random
  2. import time
  3. def displayIntro():
  4.     print 'You are on a planet full of dragons. In front of you,'
  5.     print 'you see two caves. In one cave, the dragon is friendly'
  6.     print 'and will share his treasure with you. The other dragon'
  7.     print 'is greedy and hungry, and will eat you on sight.'
  8.     print
  9. def chooseCave():
  10.     cave = ''
  11.     while cave != '1' and cave != '2':
  12.         print 'Which cave will you go into? (1 or 2)'
  13.         cave = raw_input()
  14.     return cave
  15. def checkCave(chosenCave):
  16.     print 'You approach the cave...'
  17.     time.sleep(2)
  18.     print 'It is dark and spooky...'
  19.     time.sleep(2)
  20.     print 'A large dragon jumps out in front of you! He opens his jaws and...'
  21.     print
  22.     time.sleep(2)
  23.     friendlyCave = random.randint(1, 2)
  24.     if chosenCave == str(friendlyCave):
  25.         print 'Gives you his treasure!'
  26.     else:
  27.         print 'Gobbles you down in one bite!'
  28. playAgain = 'yes'
  29. while playAgain == 'yes' or playAgain == 'y':
  30.     displayIntro()
  31.     caveNumber = chooseCave()
  32.     checkCave(caveNumber)
  33.     print 'Do you want to play again? (yes or no)'
  34.     playAgain = raw_input()

Variables

Global Scope

Local Scope

Notes

This imports the random module, which has the randint() function we will use.

Output

                 Step #1