[Tutor] Beginner question

Alan Gauld alan.gauld at btinternet.com
Mon Aug 12 02:28:13 CEST 2013


On 10/08/13 04:30, eschneider92 at comcast.net wrote:
> I've been learning python from the website 'inventwithpython.com', and
> I'm on a chapter that covers the following code:
>
> import random
> import time

> def displayIntro():
>   print('You are in a land 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 = 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 = input()


> I'm confused about what the line 'checkCave(caveNumber)' does and how it
> works. I would appreciate any help with this

It calls the checkCave function defined above. It passes in the cave 
'number' as an argument. The function basically decides whether the 
monster inside is friendly or not and prints an appropriate message.
It then returns control to the main while loop.

Does that help?

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list