【发布时间】:2022-08-17 19:50:21
【问题描述】:
在我的代码中,当我 def 一个函数时,我的变量之一出现了消息\"未使用局部变量 \'total_of_attempts\' 值\"但这对我的代码很重要。我使用 PyCharm。它在第 6 行。当我单击空格时,\'secret_number\' 变得无效,就像 \'total_of_attempts\' 一样。
import random
def play():
print(\"**********\")
print(\"Welcome to the guessing game!\")
print(\"**********\")
print(\"It\'s a very simple game. Just think about a number and take a guess. You got nothing to lose\")
secret_number = random.randrange(1, 101)
total_of_attempts = 0
points = 1000
print(\"Choose the level you want to play: \")
print(\"(1) Easy (2) Medium (3)Hard\")
level = int(input(\"Type the level: \"))
if level == 1:
total_of_attempts = 20
elif level == 2:
total_of_attempts = 10
else:
total_of_attempts = 5
for number_of_rounds in range(1, total_of_attempts + 1):
print(\"Round {} of {}\".format(number_of_rounds, total_of_attempts))
guess_str = input(\"Take a guess between 1 and 100: \")
print(\"You typed: \", guess_str)
guess = int(guess_str)
if guess < 1 or guess > 100:
print(\"You should type a number between 1 and 100\")
continue
hit = guess == secret_number
greater = guess > secret_number
lower = guess < secret_number
if hit:
print(\"Congratulations, your guess is correct and you made {} points!\".format(points))
break
else:
if greater:
print(\"You failed, your guess is greater then the secret number.\")
elif lower:
print(\"You failed, your guess is lower then the secret number.\")
lost_points = abs(secret_number - guess)
points = points - lost_points
print(\"GAME OVER\")
-
如果我复制/粘贴您的代码并运行它,则可以正常工作。有趣的小游戏。编辑:没有看到定义的功能。如果我在全局中使用
play()开始游戏,仍然可以正常工作。 -
您应该更新 pyCharm。