猿问

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

所以我试图创建一个函数 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 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))

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


凤凰求蛊
浏览 102回答 4
4回答

呼唤远方

你应该替换这个:randint&nbsp;=&nbsp;[''] random.randint(1,100)有了这个:randint&nbsp;=&nbsp;random.randint(1,100)

大话西游666

您需要将random.randint()呼叫存储到内存中。使用类似 的变量randint = random.randint(),因为它现在可以在您的代码中使用。

慕仙森

您的代码有几个问题,其中最主要的是您没有对生成的随机数做任何事情,并尝试通过 name 访问它randint,这会导致“未定义的名称”异常。你的代码的一个稍微简化的版本,也可以工作,看起来像这样:import randomtokens = [32, 45, 48, 80]def number_guess():&nbsp; &nbsp; secret = random.randint(1,100)&nbsp; &nbsp; for token in tokens:&nbsp; &nbsp; &nbsp; &nbsp; if token < secret:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('{} is too low. Random number was {}.'.format(token, secret))&nbsp; &nbsp; &nbsp; &nbsp; elif token > secret:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('{} is too high. Random number was {}.'.format(token, secret))&nbsp; &nbsp; &nbsp; &nbsp; elif token == secret:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('{} is correct!'.format(secret))if __name__ == "__main__":&nbsp; &nbsp; # Use the seed 900 to get the same pseudo random numbers every time&nbsp; &nbsp; random.seed(900)&nbsp; &nbsp; 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!

慕桂英3389331

这是对我有用的代码# TODO: Import the random moduleimport randomdef number_guess(num):&nbsp; &nbsp; # TODO: Get a random number between 1-100&nbsp; &nbsp; rand_num = random.randint(1,100)&nbsp; &nbsp; # TODO: Read numbers and compare to random number&nbsp; &nbsp; if num < rand_num:&nbsp; &nbsp; &nbsp; &nbsp; print('{} is too low. Random number was {}.'.format(num, rand_num))&nbsp; &nbsp; elif num > rand_num:&nbsp; &nbsp; &nbsp; &nbsp; print('{} is too high. Random number was {}.'.format(num, rand_num))&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; print(rand_num,"is correct!")&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;if __name__ == "__main__":&nbsp; &nbsp; # Use the seed 900 to get the same pseudo random numbers every time&nbsp; &nbsp; random.seed(900)&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; # Convert the string tokens into integers&nbsp; &nbsp; user_input = input()&nbsp; &nbsp; tokens = user_input.split()&nbsp; &nbsp; for token in tokens:&nbsp; &nbsp; &nbsp; &nbsp; num = int(token)&nbsp; &nbsp; &nbsp; &nbsp; number_guess(num)
随时随地看视频慕课网APP

相关分类

Python
我要回答