0

I am writing a code for guess the number game. Planning to use a function to decide the difficulty level. Game has two levels, I thought it would be a good idea if the program doesn't end if the user makes a wrong entry at this stage. So I formatted the function accordingly. After entering incorrect input for levels and finally entering correct level input I am getting multiple output msgs . Could you please help me understand the logic behind the multiple line output. Please note I have fixed the problem by moving the print statement displaying the attempts available inside the if and elif statements. The intension of this question is to understand why the print statement doesn't work outside the if statement.

    import random
    
    print("Welcome to the game!")
    the_number = random.choice(range(100))
    print(the_number)
    
    def decide_difficulty_level():
        difficulty_level = input("Please select a difficulty level. 'easy' or 'hard'    :").lower()
        number_of_attempts = 0
        if difficulty_level == "easy":
            number_of_attempts = 10
        elif difficulty_level == "hard":
            number_of_attempts = 5
        else:
            print("Please select a proper difficulty level. ")
            decide_difficulty_level()
        print(f"You have {number_of_attempts} attempts to guess the number. Best of luck!")
    decide_difficulty_level()

. . .

here is the output of the code as explained in the above details.

    Welcome to the game!
    94
    Please select a difficulty level. 'easy' or 'hard'    :1
    Please select a proper difficulty level. 
    Please select a difficulty level. 'easy' or 'hard'    :2
    Please select a proper difficulty level. 
    Please select a difficulty level. 'easy' or 'hard'    :3
    Please select a proper difficulty level. 
    Please select a difficulty level. 'easy' or 'hard'    :easy
    You have 10 attempts to guess the number. Best of luck!
    You have 0 attempts to guess the number. Best of luck!
    You have 0 attempts to guess the number. Best of luck!
    You have 0 attempts to guess the number. Best of luck!
    
    Process finished with exit code 0

1 Answers1

0

DON'T USE RECURSION FOR DATA VALIDATION. Your function should have one purpose, and it should return the result of its purpose, and let the caller decide what to do with it.

Also, don't do random.choice(range(xxx)). That forces Python to create the whole range. Instead, just use random.randrange or random.randint.

import random
    
print("Welcome to the game!")
the_number = random.randrange(1,100)
print(the_number)

def decide_difficulty_level():
    while 1:
        difficulty_level = input("Please select a difficulty level. 'easy' or 'hard'    :").lower()
        if difficulty_level == "easy":
            return 10
        elif difficulty_level == "hard":
            return 5
        else:
            print("Please select a proper difficulty level. ")

level = decide_difficulty_level()
print(f"You have {level} attempts to guess the number. Best of luck!")
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30