如何将其分成两个文件?
这完全有效,但是当它们位于单独的文件中时则不起作用,如下所示: main.py
from write import *
def write():
global text
text = "this is a test"
colour()
write()
color.py
import sys
from time import sleep
blue = '\033[0;34m'
def colour():
for char in text:
sleep(0.05)
sys.stdout.write(blue)
sys.stdout.write(char)
sys.stdout.flush()
sleep(0.5)
错误如下:
Traceback (most recent call last):
File "main.py", line 8, in <module>
write()
File "main.py", line 6, in write
colour()
File "/home/runner/Function-Test/colour.py", line 7, in colour
for char in text:
NameError: name 'text' is not defined
This works altogether, however doesn't when they are inside separate files, like so:
main.py
from write import *
def write():
global text
text = "this is a test"
colour()
write()
colour.py
import sys
from time import sleep
blue = '\033[0;34m'
def colour():
for char in text:
sleep(0.05)
sys.stdout.write(blue)
sys.stdout.write(char)
sys.stdout.flush()
sleep(0.5)
Error is as follows:
Traceback (most recent call last):
File "main.py", line 8, in <module>
write()
File "main.py", line 6, in write
colour()
File "/home/runner/Function-Test/colour.py", line 7, in colour
for char in text:
NameError: name 'text' is not defined
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解正确的话,我会这样做:
write.py
color.py
结果:
这是一个测试
用蓝色慢慢写。This is how I would do it if I understand correctly:
write.py
colour.py
Result:
this is a test
Written slowly in Blue.