0

I am trying to make an animal quiz using python 3.9. On one of the questions getting it wrong 3 times doesn't start a new question like it should. Instead it is just blank. On the person's first and second attempt everything works smoothly. Any and all help is appreciated. Here is my code:

def check_guess(guess, answer):
    global score
    global name
    still_guessing = True
    attempt = 0
    while still_guessing and attempt < 3:
        if guess == answer:
            print('Correct wow, i expected worse from someone named %s' % name)
            if attempt == 0:
                score = score + 3
            elif attempt == 1:
                score = score + 2
            elif attempt == 2:
                score = score + 1
            still_guessing = False
        elif attempt <= 1:
            print('HAAAAAAAAAAAAAAAAAAAAAAAAAAAA IMAGINE NOT GETTING THAT RIGHT!!!')
            print('L bozo + ratio')
            guess = input('Try again ')
            attempt = attempt + 1
    if attempt == 3:
        print('the correct answer was %s' % answer)
        print('There is always next time!!')
        still_guessing = False
score = 0
print('Welcome to the animal quiz')
print()
print('In this game you will have to guess the animal')
name = input('What is your name? ')
print('Cool name, I feel like i heard of it before hmmm %s..' % name)
print()
print('Enough stalling onto the game!!!')
guess1 = input('Which bear lives in the north pole ')
check_guess(guess1.strip().lower(), 'polar bear')
1
  • 1
    What is you expected output and real output? That is also an error. Commented Apr 2, 2022 at 5:32

5 Answers 5

2

You have to put the if attempt == 3 in your while loop, because the loop while run infinitely until the guess is right, so when attempt's value is 3, it will do nothing because it is still in a loop and there is no if statement telling it what to do once the value is 3.

EDIT: Also change the loop conds to still guessing and attempt <= 3

attempt = 0
def check_guess(guess, answer):
    global score
    global name
    global attempt
    still_guessing = True

    while still_guessing and attempt <= 3:
        print(f"attempt {attempt}")
        if attempt == 2:
            print('the correct answer was %s' % answer)
            print('There is always next time!!')
            still_guessing = False
    
        else:
            print('HAAAAAAAAAAAAAAAAAAAAAAAAAAAA IMAGINE NOT GETTING THAT RIGHT!!!')
            print('L bozo + ratio')
            guess = input('Try again ')
            attempt = attempt + 1

        if guess == answer:
            print('Correct wow, i expected worse from someone named     %s' % name)
            if attempt == 0:
                score = score + 3
            elif attempt == 1:
                score = score + 2
            elif attempt == 2:
                score = score + 1
            still_guessing = False
Sign up to request clarification or add additional context in comments.

3 Comments

tried it again still doesnt work
i edited the comment again, try again. I tested it and it worked
Also change to if attempt == 2
1

I think the elif bellow should evaluate <= 2, not 1:

        elif attempt <= 2:

But than the last 'Try again' message is still printed. You can solve that putting an attempt check condition right before the 'Try again' message. In case the condition evaluate to True, you break the loop:

        elif attempt <= 2:
            print('HAAAAAAAAAAAAAAAAAAAAAAAAAAAA IMAGINE NOT GETTING THAT RIGHT!!!')
            print('L bozo + ratio')
            attempt = attempt + 1
            if attempt > 2:
                break            
            guess = input('Try again ')

Remember to adjust you While condition in this case, as the attempt check is not necessary anymore.

If I may, I did some refactoring in the code, so you can check out other ways to achieve the same goal.

def check_guess(guess, answer, attempts):
    global score
    global name
    while True:
        if guess == answer:
            print('Correct wow, i expected worse from someone named %s' % name)
            score = [0, 1, 2, 3]
            final_score = score[attempts]
            print(f'Score: {final_score}')
            break

        attempts -= 1

        if attempts == 0:
            print('the correct answer was %s' % answer)
            print('There is always next time!!')
            break
        
        print('HAAAAAAAAAAAAAAAAAAAAAAAAAAAA IMAGINE NOT GETTING THAT RIGHT!!!')
        print('L bozo + ratio')
        guess = input('Try again ')

score = 0
print('Welcome to the animal quiz')
print()
print('In this game you will have to guess the animal')

name = input('What is your name? ')
print('Cool name, I feel like i heard of it before hmmm %s..' % name)
print()
print('Enough stalling onto the game!!!')

guess1 = input('Which bear lives in the north pole ')
check_guess(guess1.strip().lower(), 'polar bear', attempts=3)

1 Comment

that kind of works except now the person gets 4 guesses thanks though
1

I notice some mistakes. First, you don't need if attempt == 3 or 2 because you are using a while loop (while loop is based on the condition at first). Second, it is better to integrate "break" to not have an infinite loop. While loop starts from 0 and ends at 2 (takes 3 values).

I rewrote the code, for you.

def check_guess(guess, answer):
global score
global name
still_guessing = True
attempt = 0
while still_guessing and attempt < 2:
    if guess == answer:
        print('Correct wow, i expected worse from someone named %s' % name)
        if attempt == 0:
            score = score + 3
        elif attempt == 1:
            score = score + 2
        elif attempt == 2:
            score = score + 1
        still_guessing = False
    elif attempt <= 1:
        print('HAAAAAAAAAAAAAAAAAAAAAAAAAAAA IMAGINE NOT GETTING THAT RIGHT!!!')
        print('L bozo + ratio')
        guess = input('Try again ')
        attempt = attempt + 1
    break

print('the correct answer was %s' % answer)
print('There is always next time!!')
still_guessing = False

To test the code add print("pass OK")

score = 0
print('Welcome to the animal quiz')
print()
print('In this game you will have to guess the animal')
name = input('What is your name? ')
print('Cool name, I feel like i heard of it before hmmm %s..' % name)
print()
print('Enough stalling onto the game!!!')
guess1 = input('Which bear lives in the north pole ')
check_guess(guess1.strip().lower(), 'polar bear')
print("pass OK")

Don't forget to upvote :) (Ramadan Karim!!!)

Comments

0

in your while loop 'attempt' never become 3, so the code can't jump to the next part if attmpt == 3 so in the while loop, the elif condition should be elif attempt <= 2: then attempt = attempt + 1 can reach 3

Comments

0

Use below code it works for me .

def check_guess(guess, answer):
    global score
    global name
    still_guessing = True
    attempt = 0
    while still_guessing and attempt < 3:
        if guess == answer:
            print('Correct wow, i expected worse from someone named %s' % name)
            if attempt == 0:
                score = score + 3
            elif attempt == 1:
                score = score + 2
            elif attempt == 2:
                score = score + 1
            still_guessing = False
        elif attempt <= 2:
            print('HAAAAAAAAAAAAAAAAAAAAAAAAAAAA IMAGINE NOT GETTING THAT RIGHT!!!')
            print('L bozo + ratio')
            guess = input('Try again ')
            attempt = attempt + 1
    if attempt == 3:
        print('the correct answer was %s' % answer)
        print('There is always next time!!')
        still_guessing = False
score = 0
print('Welcome to the animal quiz')
print()
print('In this game you will have to guess the animal')
name = input('What is your name? ')
print('Cool name, I feel like i heard of it before hmmm %s..' % name)
print()
print('Enough stalling onto the game!!!')
guess1 = input('Which bear lives in the north pole ')
check_guess(guess1.strip().lower(), 'polar bear')

I have change 1 into 2.

1 Comment

While you fixed the bug, you leave it up to the reader to hunt down what you changed. You could improve your answer by showing what you changed and why it fixes the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.