所以我试图创建一个函数 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))
但我并不真正理解格式以及它的功能应该如何工作。任何帮助表示赞赏!
呼唤远方
大话西游666
慕仙森
慕桂英3389331
相关分类