【问题标题】:How to generate random numbers and compare them with token inputs?如何生成随机数并将它们与令牌输入进行比较?
【发布时间】:2020-09-17 15:40:21
【问题描述】:

所以我正在尝试创建一个函数 def number_guess(num): 以生成一些随机整数并将它们与某些输入() 进行比较并打印一些语句。

例如,如果我输入:

32 45 48 80

我的目标输出是

32 is too low. Random number was 80.
45 is too high. Random number was 30.
48 is correct!
80 is too low. Random number was 97.

我们也使用种子值 900,这将导致计算机在每次程序运行时选择相同的随机数。

到目前为止,我的代码是:

# TODO: Import the random module
import random

def number_guess(num):
    # TODO: Get a random number between 1-100

    random.randint(1,100)

    # TODO: Read numbers and compare to random number

    for token in tokens:
        if token < randint:
            print('{} is too low. Random number was {}.'.format(user_input[0], randint))
        elif token > randint:
            print('{} is too high. Random number was {}.'.format(user_input[1], randint))
        elif token == randint:
            print('{} is correct!'.format(randint))


if __name__ == "__main__":
    # Use the seed 900 to get the same pseudo random numbers every time
    random.seed(900)

    # Convert the string tokens into integers
    user_input = input()
    tokens = user_input.split()
    for token in tokens:
        num = int(token)
        number_guess(num)

我试过了: def number_guess(num): # TODO:获取1-100之间的随机数

    randint = ['']
    random.randint(1,100)

    # TODO: Read numbers and compare to random number

    for num in tokens:
        if token < randint:
            print('{} is too low. Random number was {}.'.format(num[0], randint))
        elif token > randint:
            print('{} is too high. Random number was {}.'.format(num[1], randint))
        elif token == randint:
            print('{} is correct!'.format(randint))

但我并不真正了解格式以及此功能应如何工作。任何帮助表示赞赏!

【问题讨论】:

  • randint = random.randint(1,100)

标签: python


【解决方案1】:

您需要将您的random.randint() 调用存储到内存中。

使用像randint = random.randint() 这样的变量,因为它现在可以在您的代码中使用。

【讨论】:

    【解决方案2】:

    您的代码有几个问题,主要是您没有对生成的随机数做任何事情,并尝试通过名称randint 访问它,这将导致“未定义名称”异常。

    你的代码的一个稍微简化的版本,也可以工作,看起来像这样:

    import random
    
    tokens = [32, 45, 48, 80]
    
    def number_guess():
        secret = random.randint(1,100)
        for token in tokens:
            if token < secret:
                print('{} is too low. Random number was {}.'.format(token, secret))
            elif token > secret:
                print('{} is too high. Random number was {}.'.format(token, secret))
            elif token == secret:
                print('{} is correct!'.format(secret))
    
    if __name__ == "__main__":
        # Use the seed 900 to get the same pseudo random numbers every time
        random.seed(900)
        number_guess()
    

    我删除了用户输入部分、不相关的参数和一个无关的循环,现在您可以得到所有测试令牌的反馈:

    32 is too low. Random number was 80.
    45 is too low. Random number was 80.
    48 is too low. Random number was 80.
    80 is correct!
    

    【讨论】:

    • 如果我不想要定义的标记列表,只需要用户输入的整数怎么办?
    • @yeobub 在这种情况下,您可以执行tokens = [input1, input2, input3, input4],其中每个变量都被提前提升。
    • @yeobub - 你的问题中已经有代码,你只是错误地使用了检索到的令牌列表。
    【解决方案3】:

    你应该替换这个:

    randint = ['']
    random.randint(1,100)
    

    用这个:

    randint = random.randint(1,100)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-19
      • 2021-06-17
      • 1970-01-01
      • 2020-07-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多