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