repeat program

E

eschneider92

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.
 
D

Denis McMahon

How do I make the following program repeat twice instead of asking
whether the player wants to play again?

You change something in the folowing two lines:

playagain='yes'
while playagain=='yes':

What you change and how you change it is probably your homework task, so
I really shouldn't tell you any more than that.

Also, I might not be right about where you need to make the change, but
hey, this is the internet, it must be right, yeah?
 
D

Dave Angel

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()
 
M

MRAB

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'?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top