[Tutor] Beginner question

Ciaran Mooney dancingbush at gmail.com
Mon Aug 12 11:49:35 CEST 2013



On 10 Aug 2013, at 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
> 
> Thank you,
> 
> Eric
> 

Hi Eric,

This line calls the method 'checkCave()'. This method takes the argument caveNumber (checkCave(caveNumber) , which is a string returned from user input generated in the method chooseCave().

In checkCave method, the value (caveNumber) is then used in a if/else statement to compare with a random integer (parsed to a string str(frindlyCave))  to determine whether you will be eaten or given treasure ;)

Hope that was of some help. 

Ciaran 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130812/7b54f0a6/attachment.html>


More information about the Tutor mailing list