如何修复'NameError:name'random'未定义'error in Python3

2024-10-05 12:27:50 发布

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

不久前,我用python3编写了一个程序,此后我对在一个单独的python3程序中运行这个程序很感兴趣(我使用了exec(open("filename.py").read())来实现这一点)。在

但是,在运行原始程序的独立程序中,它会给出以下错误消息:NameError: name 'random' is not defined。我肯定是随机导入的,所以我不明白它为什么要给我这个消息。我认为这应该是节目的重要内容:

#!/usr/bin/env python3

import random, time  # the top lines of the program

if random.randint(1, 100) >= 36:  # the line which the error message points me to
    ... 

老实说,我不知道我能在这里做什么-任何帮助将不胜感激。在


Tags: thenamepy程序消息readis错误

热门问题