【问题标题】:Python- If statements with random numbers not workingPython-带有随机数的If语句不起作用
【发布时间】:2018-07-31 16:58:26
【问题描述】:

我对编程很陌生,我正在尝试在 python 3.6 中编写一个程序,它生成一个随机数作为“答案”,然后计算机必须猜测 x 个问题的答案。为了跟踪计算机猜对了多少问题,我创建了一个名为“正确”的变量,如果计算机的答案等于猜测,则在该变量中添加一个。但是,即使它是错误的,它也会每次都这样做。抱歉,如果这看起来很愚蠢,但感谢您的帮助

import random
def total(x, t):
        for i in range(t):
                cor = 0
                gue = 0
                n = 0
                right = 0
                def ans():
                         cor = random.randint(1,4)
                         print(cor, 'answer')
                def guess():
                         gue = random.randint(1,4)
                         print(gue, 'guess')
                while n <= x:
                         ans()
                         guess()
                         if cor == gue:
                                 right += 1
                         n += 1 
                print(right, n)

【问题讨论】:

  • if cor == guecorgue 行中始终为零,因此它们始终相等。在ans()guess() 函数中更改这些变量只会在该函数内更改它们。有一些方法可以解决这个问题(例如将这些变量声明为非本地变量),但最好的方法是返回值。
  • 谢谢,我取出了第一部分,我将 cor 和 gue 定义为 0,并在 ans() 和 guess() 中将它们设为全局。现在可以了,谢谢你的帮助

标签: python if-statement random


【解决方案1】:

我试图通过将ans()guess() 函数移到total(x,t) 函数之外来稍微修改您的代码,因为它们看起来彼此独立。以前ansguess 随机生成corgue,但不返回它们的值以在始终使用0 的初始值的if 语句中使用。现在通过将返回值保存为cor = ans()gue = guess()0 的初始化值corgue 将被覆盖。

import random

def ans():
    cor = random.randint(1,4)
    print(cor, 'answer')
    return cor

def guess():
    gue = random.randint(1,4)
    print(gue, 'guess')
    return gue

def total(x, t):
    for i in range(t):
        cor = 0
        gue = 0
        n = 0
        right = 0
        while n <= x:
            cor = ans()
            gue = guess()
            if cor == gue:
                     right += 1
            n += 1 
    print(right, n)


print (total(2,3))

输出

1 answer
2 guess
3 answer
4 guess
1 answer
4 guess
0 3
3 answer
2 guess
1 answer
4 guess
3 answer
2 guess
0 3
2 answer
2 guess
2 answer
3 guess
3 answer
4 guess
1 3

【讨论】:

  • 谢谢,我把它们移到外面了,现在可以正常使用了。感谢您的帮助!
  • 对您喜欢的答案进行投票以感谢帮助您的人的时间/努力
  • 我没有足够高的声誉来展示它:(
【解决方案2】:

ansguess 这样的嵌套函数可能很有用,但在开始编程时我会避开它。它使您的程序更难理解。

这是我对你正在尝试做的事情的抨击(如果我理解正确的话!)

import random

# Play number_of_games_to_play of the number guessing game allowing a maximum of max_guesses per game played.
def play(number_of_games_to_play, max_guesses):
    num_games = 0
    guessed_right = 0
    while num_games < number_of_games_to_play:
        if guess_number(max_guesses):
            guessed_right += 1
        num_games += 1
    print('\n\nI played', num_games, 'games and guessed right', guessed_right, 'times')

# Generate a random number, try and guess it up to max_guesses times. Returns whether the number was guessed correctly or not.
def guess_number(max_guesses):
    answer = random.randint(1, 4)
    print('\n\nPlaying guess_number. The right answer is', answer)
    num_guesses = 0
    while num_guesses < max_guesses:
        guess = random.randint(1, 4)
        print('The computer guesses', guess)
        if guess == answer:
            print('That is right!')
            return True
        else:
            print('That is wrong')
        num_guesses += 1
    return False

play(number_of_games_to_play = 5, max_guesses = 4)

【讨论】:

  • 对不起,我说得不清楚,我想做的是类似于多项选择测试,看看计算机能猜对多少问题。 X 是问题的数量,T 是计算机模拟测试的次数。感谢您的帮助,这是一个有趣的变化
【解决方案3】:

我认为您对变量的范围有疑问:函数内部的corgue 与它们外部的不同。您的函数不会更改 corgue 的值,而是创建两个仅存在于其中的新变量(也称为 corgue)。因此,corgue 始终为 0,if 语句中的条件始终为真,并且您每次递增 rigth。 要解决此问题,您可以将变量作为参数传递给函数。

【讨论】:

    【解决方案4】:

    问题来自变量范围。方法 ans 中定义的变量 core 与 for 循环中初始化的变量 core 不同。 更好的方法是:

    随机导入

    def ans():
        c = random.randint(1, 4)
        return c
    
    
    def guess():
        g = random.randint(1, 4)
        return g
    
    
    def total(x, t):
        for i in range(t):
            n = 0
            right = 0
            while n <= x:
                cor = ans()
                gue = guess()
                if (cor == gue):
                    right += 1
                print("Answer: %6d Guess: %6d Right: %d" % (cor, gue, right))
                n += 1
            print(right, n)
    

    【讨论】:

    • 谢谢,这看起来比我的原版好多了!我还在学习,感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-29
    • 1970-01-01
    相关资源
    最近更新 更多