repeat program

Dave Angel davea at davea.name
Mon Apr 29 20:38:41 EDT 2013


On 04/29/2013 08:22 PM, eschneider92 at comcast.net wrote:
> How do I make the following program repeat twice instead of asking whether the player wants to play again?
>

Turn it into a function call, and call that function twice from 
top-level.  Or, more generally,



for i in range(2):
     doit()


>
> import random
> import time
>
> def intro():
>      print('You spot 2 caves in the distance.')
>      print ('You near 2 cave entrances..')
>      time.sleep(1)
>      print('You proceed even nearer...')
>      time.sleep(1)
>
> def choosecave():
>      cave=''
>      while cave!='1' and cave !='2':
>          print('which cave?(1 or 2)')
>          cave=input()
>          return cave
>
> def checkcave(chosencave):
>      friendlycave=random.randint(1,2)
>      if chosencave==str(friendlycave):
>          print ('you win')
>      else:
>          print('you lose')
>
> playagain='yes'
> while playagain=='yes':
>      intro()
>      cavenumber=choosecave()
>      checkcave(cavenumber)
>      print('wanna play again?(yes no)')
>      playagain=input()
>
> Thanks in advance.
>


-- 
DaveA



More information about the Python-list mailing list