简体   繁体   English

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

[英]How to generate random numbers and compare them with token inputs?

So i'm trying to make a function def number_guess(num): in order to generate some random integers and compare them with certain inputs() and print some statements.所以我试图制作一个 function def number_guess(num): 为了生成一些随机整数并将它们与某些输入()进行比较并打印一些语句。

For ex if i input:例如,如果我输入:

32 45 48 80

My target output is我的目标 output 是

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.

We are also using the seed value 900, which will cause the computer to choose the same random number every time the program runs.我们还使用种子值 900,这将导致计算机在每次程序运行时选择相同的随机数。

So far my code is:到目前为止,我的代码是:

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

I tried: def number_guess(num): # TODO: Get a random number between 1-100我试过: 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))

But i'm not really understanding the format and how the functions for this should work.但我并不真正了解格式以及此功能应该如何工作。 Any help is appreciated!任何帮助表示赞赏!

You should replace this:你应该替换这个:

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

with this:有了这个:

randint = random.randint(1,100)

You need to store your random.randint() call to memory.您需要将random.randint()调用存储到 memory。

Use a variable like randint = random.randint() , since it will work in your code now.使用像randint = random.randint()这样的变量,因为它现在可以在您的代码中使用。

There were several issues with your code, the chief of which was that you did not do anything with the random number you generated, and tried accessing it through the name randint , which would cause an "undefined name" exception.您的代码有几个问题,主要是您没有对生成的随机数做任何事情,并尝试通过名称randint访问它,这将导致“未定义名称”异常。

A somewhat simplified version of you code, that will also work, would look like this:你的代码的一个稍微简化的版本,也可以工作,看起来像这样:

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

I removed the user input part, irrelevant parameters, and an extraneous loop, and now you get feedback on all the tested tokens:我删除了用户输入部分、不相关的参数和一个无关的循环,现在您可以获得所有测试令牌的反馈:

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!

This is the code that works for me这是对我有用的代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM