4 回答
TA贡献1856条经验 获得超11个赞
你应该替换这个:
randint = [''] random.randint(1,100)
有了这个:
randint = random.randint(1,100)
TA贡献1817条经验 获得超14个赞
您需要将random.randint()
呼叫存储到内存中。
使用类似 的变量randint = random.randint()
,因为它现在可以在您的代码中使用。
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!
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)
添加回答
举报