Home > database >  Python if and else statement not expected output
Python if and else statement not expected output

Time:04-02

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

CodePudding user response:

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

CodePudding user response:

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)

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

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!!!)

  • Related