为了账号安全,请及时绑定邮箱和手机立即绑定

如何生成随机数并将它们与令牌输入进行比较?

如何生成随机数并将它们与令牌输入进行比较?

凤凰求蛊 2022-12-14 21:14:33
所以我试图创建一个函数 def number_guess(num): 以生成一些随机整数并将它们与某些 inputs() 进行比较并打印一些语句。例如,如果我输入: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 moduleimport randomdef 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))但我并不真正理解格式以及它的功能应该如何工作。任何帮助表示赞赏!
查看完整描述

4 回答

?
呼唤远方

TA贡献1856条经验 获得超11个赞

你应该替换这个:

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

有了这个:

randint = random.randint(1,100)


查看完整回答
反对 回复 2022-12-14
?
大话西游666

TA贡献1817条经验 获得超14个赞

您需要将random.randint()呼叫存储到内存中。

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


查看完整回答
反对 回复 2022-12-14
?
慕仙森

TA贡献1827条经验 获得超8个赞

您的代码有几个问题,其中最主要的是您没有对生成的随机数做任何事情,并尝试通过 name 访问它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!


查看完整回答
反对 回复 2022-12-14
?
慕桂英3389331

TA贡献2036条经验 获得超8个赞

这是对我有用的代码


# TODO: Import the random module

import random


def number_guess(num):

    # TODO: Get a random number between 1-100

    rand_num = random.randint(1,100)

    # TODO: Read numbers and compare to random number

    if num < rand_num:

        print('{} is too low. Random number was {}.'.format(num, rand_num))

    elif num > rand_num:

        print('{} is too high. Random number was {}.'.format(num, rand_num))

    else:

        print(rand_num,"is correct!")

        

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)


查看完整回答
反对 回复 2022-12-14
  • 4 回答
  • 0 关注
  • 101 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信