不久前,我用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
...
老实说,我不知道我能在这里做什么-任何帮助将不胜感激。在
使代码成为函数,然后在代码末尾使用
if __name__ == '__main__':
:相关问题 更多 >
编程相关推荐