【问题标题】:Local variable 'total_of_attempts' value is not used未使用局部变量 \'total_of_attempts\' 值
【发布时间】: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。

标签: python pycharm ide


【解决方案1】:

根据评论,对您的游戏进行一些测试:

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()

结果:

**********
Welcome to the guessing game!
**********
It's a very simple game. Just think about a number and take a guess. You got nothing to lose
Choose the level you want to play:
(1) Easy  (2) Medium  (3)Hard
Type the level: 1
Round 1 of 20
Take a guess between 1 and 100: 50
You typed:  50
You failed, your guess is greater then the secret number.
Round 2 of 20
Take a guess between 1 and 100: 25
You typed:  25
You failed, your guess is lower then the secret number.
Round 3 of 20
Take a guess between 1 and 100: 40
You typed:  40
You failed, your guess is greater then the secret number.
Round 4 of 20
Take a guess between 1 and 100: 30
You typed:  30
You failed, your guess is lower then the secret number.
Round 5 of 20
Take a guess between 1 and 100: 35
You typed:  35
You failed, your guess is greater then the secret number.
Round 6 of 20
Take a guess between 1 and 100: 31
You typed:  31
You failed, your guess is lower then the secret number.
Round 7 of 20
Take a guess between 1 and 100: 32
You typed:  32
Congratulations, your guess is correct and you made 961 points!
GAME OVER

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多