Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

print statement only when the condition is CONSECUTIVELY met 3 times in a row

I’m currently making a guessing game where user can get a congrats statement if they guess correctly 3 times in a row or a hint statement if they guesses incorrectly 3 times in a row. If user makes two correct guesses and one incorrect guess the count will reset and vice versa for incorrect guesses. the goal is for the right/wrong guess to be 3 times in a row for the statement to print

Here is what I have

count = 0

rightGuess = 0

wrongGuess = 0

die1 = random.randint(1,6)



guess = int(input('Enter guess: '))

if guess == die1:
     
rightGuess += 1
     
print('Good job')
     
     if rightGuess == 3:
          print('You guessed three times in a row!')


if guess != die1:
      
wrongGuess += 1
      
print('Uh oh wrong answer')
     
      if wrongGuess == 3:
           print("Hint: issa number :)")

This works but it displays the text whenever the user reaches 3 wrong or right guesses even if it’s not in a row. Please help

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You just have to reset the opposite variable to 0 when incrementing either of them.

count = 0
consecutiveRightGuess = 0
consecutiveWrongGuess = 0
die1 = random.randint(1, 6)
guess = int(input('Enter guess: '))

if guess == die1:
    consecutiveWrongGuess = 0
    consecutiveRightGuess += 1
    print('Good job')
    if consecutiveRightGuess == 3:
        print('You guessed three times in a row!')

if guess != die1:
    consecutiveRightGuess = 0
    consecutiveWrongGuess += 1
    print('Uh oh wrong answer')
    if consecutiveWrongGuess == 3:
        print("Hint: issa number :)")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading