Issue
import random
level = input("Easy gets 10 attempt Hard getse 5 attempt choose a level --> ").lower()
number = random.choice(range(101))
attempt = 0
if level == "easy":
attempt = 10
else:
attempt = 5
def check_number(guess,num):
if guess == num:
return "Correct You Won :)"
win = True
elif guess > num:
return "Too high :("
elif guess < num:
return "Too low :("
win = False
while attempt > 0 and win == False:
guess = int(input("Guess the number "))
print(check_number(guess,number))
attempt -= 1
print(f"You have {attempt} attempt left")
if win == True:
print(f"GOOD JOB YOU HAVE {attempt} LEFT")
else:
print("NEXT TİME :(")
It does not stop when you guess the number, still asks for new guess. I couldn't find the problem. I just started learning python.
Solution
This avoids using a global variable at all:
import random
level = input("Easy gets 10 attempt Hard getse 5 attempt choose a level --> ").lower()
number = random.choice(range(101))
attempt = 0
if level == "easy":
attempt = 10
else:
attempt = 5
def check_number(guess,num):
if guess > num:
return False, "Too high :("
elif guess < num:
return False, "Too low :("
return True, "Correct You Won :)"
win = False
while attempt > 0 and not win:
guess = int(input("Guess the number "))
win, msg = check_number(guess,number)
print(msg)
if not win:
attempt -= 1
print(f"You have {attempt} attempt left")
if win:
print(f"GOOD JOB YOU HAVE {attempt} LEFT")
else:
print("NEXT TİME :(")
Answered By - Tim Roberts
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.