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

What is the alternative way to rewrite the below code without using global variables in Python?

There is an animal quiz program. The program asks the player some questions about animals.
They get three chances to answer each question. Each correct answer will
score one point. At the end of the quiz, the program reveals
the player’s final score.
The code is here:

score = 0
def check_guess(guess, answer):
    global score
    still_guessing = True
    attempt = 0
    while still_guessing and attempt < 3:
        if guess.lower() == answer.lower():
            print('Correct answer')
            score = score + 1
            still_guessing = False
        else:
            attempt = attempt + 1
            
            if attempt < 3:
                guess = input('Sorry wrong answer. Try again. ')                    
    if attempt == 3:
        print('The correct answer is ' + answer)

print('Guess the Animal!')
guess1 = input('Which bear lives at the North Pole? ')
check_guess(guess1, 'polar bear')
guess2 = input('Which is the fastest land animal? ')
check_guess(guess2, 'cheetah')
guess3 = input('Which is the largest animal? ')
check_guess(guess3, 'blue whale')
print('Your score is ' + str(score))

I know it is not a good practice to use ‘global’ variables. So I used another way to make it work: pass the arguments to the function. It looks like this:

score = 0
def check_guess(guess, answer, score):    
    still_guessing = True
    attempt = 0
    while still_guessing and attempt < 3:
        if guess.lower() == answer.lower():
            print('Correct answer')
            score = score + 1
            still_guessing = False
        else:
            attempt = attempt + 1
            
            if attempt < 3:
                guess = input('Sorry wrong answer. Try again. ')                    
    if attempt == 3:
        print('The correct answer is ' + answer)
    return score

print('Guess the Animal!')
guess1 = input('Which bear lives at the North Pole? ')
score1 = check_guess(guess1, 'polar bear', score)
guess2 = input('Which is the fastest land animal? ')
score2 = check_guess(guess2, 'cheetah', score1)
guess3 = input('Which is the largest animal? ')
score3 = check_guess(guess3, 'blue whale', score2)
print('Your score is ' + str(score3))

But I feel the way I did is a little bit complex. So I just wonder if there is a better way to do it without using ‘global’ variables, or in this case, to use ‘global’ variable is the best solution?

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 :

There isn’t really any need for the functions to interact with the global score, you just need it to determine the score for one question. I’ve also simplified your looping:

score = 0
def check_guess(guess, answer):
    for attempt in range(3):
        if guess.lower() == answer.lower():
            print('Correct answer')
            return 1
        guess = input('Sorry wrong answer. Try again. ')                    
    print('The correct answer is ' + answer)
    return 0

print('Guess the Animal!')
guess1 = input('Which bear lives at the North Pole? ')
score += check_guess(guess1, 'polar bear')
guess2 = input('Which is the fastest land animal? ')
score += check_guess(guess2, 'cheetah')
guess3 = input('Which is the largest animal? ')
score += check_guess(guess3, 'blue whale')
print('Your score is ' + str(score))
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