'is the global constant cannot be use in while loops?
is the global constant cannot be use in while loops?
EASY_ATTEMP = 10
HARD_ATTEMP = 5
random_number = random.randint(1, 100)
difficulty = "easy"
if difficulty == "easy":
attemp = EASY_ATTEMP
already_finished = False
while not already_finished:
print(f"You have {attemp} attempts reamaining to guess the number") #<--here
guess = int(input("Make a guess: "))
And the its show the error like this:
Traceback (most recent call last):
File "main.py", line 21, in <module>
print(f"You have {attemp} attempts reamaining to guess the number")
NameError: name 'attemp' is not defined
What is going on?
Solution 1:[1]
I copied the code and tried run it but got no errors. I don't know what the problem is but I would suggest to make your code neater. '''
attemp = 5
random_number = random.randint(1, 100)
difficulty = input('Do you want easy difficulty? Y or N?')
if difficulty in "Yy":
attemp = 10
while attemp > 0:
print(f"You have {attemp} attempts reamaining to guess the number") #<--here
guess = int(input("Make a guess: "))
if guess == random_number:
break
attemp -= 1
'''
For example, in the while loop, you can create a boolean with the attempt variable and reduce the number by one for ever iteration. The variable 'attemp' will always evaluate to an easy difficulty because the variable difficulty is always referencing the 'easy' str. I'm not sure how you want the code to run but in the above code I set it so that the user can decide.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ethan _kom |
