Home > Blockchain >  how to loop an input function until score is found
how to loop an input function until score is found

Time:10-13

I am learning python and I am at 'interacting functions'. This is the 'find the ball game' where I want to loop the input until the correct glass is chosen in 'check_guess' function

to shuffle the list

def shuffle_list(mylist):
    shuffle(mylist)
    return mylist

3 glasses and one has the ball

mylist = ['','O','']

interacting the functions

def check_guess(mylist, guess):
    
    if mylist[guess] == 'O':
        print('Correct')
                
    else:
        print('Wrong Guess')
        print(mylist)

#INITIAL LIST

mylist = ['','O','']

#SHUFFLE LIST

mixedup_list = shuffle_list(mylist)

#USER GUESS

guess = player_guess()

#CHECK GUESS

check_guess(mixedup_list,guess)

CodePudding user response:

Wrap them in a while loop. You can use break to break out of the loop whenever you want.

CodePudding user response:

Modify your check_guess function so it returns True when the guess is correct and then build a while loop around the player_guess function.

def check_guess(mylist, guess):
    
    if mylist[guess] == 'O':
        print('Correct')
        return True
                
    else:
        print('Wrong Guess')
        print(mylist)
        return False

check = False
while check == False:
    guess = player_guess()
    check = check_guess(mixedup_list, guess)

CodePudding user response:

Put the code in a while True loop, and break out of the loop when the correct answer is chosen.

while True:
    # shuffle the balls
    # ask for a guess
    if guess is correct:
        print('Correct!')
        break
    else:
        print('Too bad, try again')
        # loop will repeat
  • Related