【发布时间】:2021-12-29 21:02:47
【问题描述】:
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 :(")
当你猜数字时它不会停止,仍然要求新的猜测。 我找不到问题。我刚开始学习python。
【问题讨论】:
-
你函数中的
win是一个局部变量,没有连接到win全局变量。当然,这没关系,因为在您返回之前,您不会将win设置为 True。我会在下面发布一个更好的设计。 -
不要使用
globals,它们可能会导致更新变量时出错。
标签: python python-3.x