Sun Varunpaijit
Sun Varunpaijit

Reputation: 1

How to repeat a function 3 times

I'm creating this function to test if a user input(as a guess) is correct or not.

def check_guess():
   letter = "d"
   guess = input("What is your guess: ")
   if guess.isalpha() == False:
       print("This is invalid")
   elif guess.lower() > letter:
       print("This too high")
   elif guess.lower() < letter:
       print("this is too low")
   else:
       print("that is correct")

check_guess()

So I created this code, and it works no problem. However, I am now tasked with having to give the user 3 attempts. If the user gets the correct answer, then the "that is correct" will be printed and the game ends. But if they fail on all 3 attempts, then it's something like "Gameover".

How do I create/duplicate the code to make it able to do that?

Upvotes: 0

Views: 1499

Answers (5)

Joe
Joe

Reputation: 12417

You can do in this way:

def check_guess():
    status = False
    letter = "d"
    guess = input("What is your guess: ")
    if guess.isalpha() == False:
        print("This is invalid")
    elif guess.lower() > letter:
        print("This too high")
    elif guess.lower() < letter:
        print("this is too low")
    else:
        print("that is correct")
        status = True
    return status

for i in range(3):
    status = check_guess()
    if status:
        break
else:
    print "Gameover"

Upvotes: 1

Mad Physicist
Mad Physicist

Reputation: 114310

Use a for loop and its else clause. The else clause of a loop only runs if you didn't break out of the loop.

Start by having your function return a value to indicate whether the user guessed correctly or not. Otherwise it'll be very difficult to interact with it:

def check_guess():
    letter = "d"
    guess = input("What is your guess: ")
    if not guess.isalpha():
        print("This is invalid")
        return False
    guess = guess.lower()
    if guess == letter:
        print("that is correct")
        return True
    if guess > letter:
        print("This too high")
    else:
        print("this is too low")
    return False

Now you can call the function exactly three times, or until the user guesses correctly, whichever comes first:

for _ in range(3):
    if check_guess():
        break
else:
    print('you failed')

When check_guess returns True to indicate success, we break out of the loop, ensuring that the else clause is not triggered. If the three iterations complete and the user never made a correct guess, the clause is triggered.

Upvotes: 3

Rivers Shall
Rivers Shall

Reputation: 561

def check_guess():
   letter = "d"
   guess = input("What is your guess: ")
   if guess.isalpha() == False:
       print("This is invalid")
       return False
   elif guess.lower() > letter:
       print("This too high")
       return False
   elif guess.lower() < letter:
       print("this is too low")
       return False
   else:
       print("that is correct")
       return True

for i in range(0,3):
     status = check_guess()
     If status:
         print(“success”)
         break
else:
     print(“fail”)

Upvotes: 2

Spoutnovitch
Spoutnovitch

Reputation: 119

You need to return if the user was right, and then if not increment a counter. Something like this should work:

count = 0
while count < 3:
    if check_guess():
        # he's right
        break
    else:
        # he's wrong
        count += 1
if count >= 3:
    print("You lose !")
else:
    print("You win !")

Upvotes: 1

Arkaprabha Mahata
Arkaprabha Mahata

Reputation: 65

 c=0
 def check_guess():
 letter = "d"
 guess = input("What is your guess: ")
 if guess.isalpha() == False:
   print("This is invalid")
   c++
 elif guess.lower() > letter:
   print("This too high")
   c++
 elif guess.lower() < letter:
   print("this is too low")
   c++
 else:
   print("that is correct")
   c=4
 if c<=3
   check_guess()

Upvotes: -2

Related Questions