我的python代码中有一些打印问题。
在这段代码中,我有一个部分,我有多个print语句,导致在控制台中输出shiftet。
部分代码:
print ("Text 1")
print ("Text 2")
print ("Text 3")
print ("Text 4")
print ("Text 5")
我期待的结果如下:
Text1
Text2
Text3
Text4
Text5
相反,我会得到类似的东西:
Text1
Text2
Text3
Text4
Text5
有人可以解释我以及如何摆脱它吗? 我需要说这只发生在程序启动期间,因为我有一个关键输入的监听器。因此,当我按下一个键几次时,然后在每次按下之后正确地切换某个点,如下所示:
Text1
Text2
Text3
Text4
Text5
Key pressed
Key pressed
Key pressed
Key pressed
Key pressed
编辑: 这是一个使用python 2.7版本生成这样的outpu的最小代码
import sys
import termios
import tty
import pigpio
import time
from thread import start_new_thread
abort = False
pi = pigpio.pi()
def getCh():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(fd)
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
def checkKey():
global abort
while True:
c = getCh()
if c == 'x' and not abort:
abort = True
break
start_new_thread(checkKey, ())
print ("+ / - = Increase / Decrease brightness (each by 10)")
print ("f / s = Increase / Decrease cycle speed")
print ("c = Switch cycle speed steps")
print ("p / r = Pause / Resume")
print ("i = Print info")
print ("x = Abort Program")
while abort == False:
time.sleep(0)
print ("Aborting...")
pi.stop()