這完全有效,但是當它們在單獨的檔案中時不起作用,如下所示:main.py
from write import *
def write():
global text
text = "this is a test"
colour()
write()
顏色.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
uj5u.com熱心網友回復:
如果我理解正確,我會這樣做:
寫.py
from colour import colour
def write():
text = "this is a test"
colour(text)
write()
顏色.py
import sys
from time import sleep
blue = '\033[0;34m'
def colour(text):
for char in text:
sleep(0.05)
sys.stdout.write(blue)
sys.stdout.write(char)
sys.stdout.flush()
sleep(0.5)
結果:
this is a test用藍色慢慢寫。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/429750.html
