repeat program

MRAB python at mrabarnett.plus.com
Mon Apr 29 20:43:08 EDT 2013


On 30/04/2013 01:22, eschneider92 at comcast.net wrote:
> How do I make the following program repeat twice instead of asking whether the player wants to play again?
>
>
> 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.
>
Replace the 'while' loop with a 'for' loop that loops twice.

By the way, there's a bug in 'choosecave': what happens if the user
enters, say, '3'?




More information about the Python-list mailing list