使用random.randin时未定义“名称'random'”

2024-05-14 11:10:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个Python脚本,它将从我编写的列表中打印出一个随机值

from random import randint
class_list=[0,1,2,3,4,5,5,6,7,8,9,10,11,12]
people_called=[]
randomized_number = random.randint(0,12)
print "debug number" + str(randomized_number)
print "The student is" + str(class_list[randomized_number])
class_list[randomized_number].append(people_called)

但是当我运行这个文件时

Traceback (most recent call last):
  File "./Code/class list.py", line 4, in <module>
    number = random.randint(0,12)
NameError: name 'random' is not defined

Tags: fromimport脚本number列表israndompeople
2条回答

from random import randintrandom模块导入randint。也就是说,您可以将其用作randint。如果要将其导入为import random,则必须使用random.randint

使用just import random导入时,必须使用random.randint()调用函数。

当使用from random import randint时,Python允许您仅使用randint()调用函数

相关问题 更多 >

    热门问题