在while循环中添加一些字符

2024-09-27 07:20:08 发布

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

我在while循环中读取串行数据,我想在代码中添加一些东西,如果我按t,那么应该打印一些东西,否则while循环应该可以工作。我试图使用readchar,但它等着我按一些键,我不想让它等。我想在按下某个键之前继续工作

树莓皮2.7

while True:
    data = s.recv(xxx)
    print(data)

    if (x == t)
        print(Hello)
    else:
        continue

如果我使用read char,它正在等待。你知道吗


Tags: 数据代码truehelloreaddataifelse
1条回答
网友
1楼 · 发布于 2024-09-27 07:20:08

已经有很多类似的问题可以解决你的问题。其中包括:

Loubo's "detect key press in python" here on SO,以及Spae的"Python key press, detect (in terminal, no enter or pause)"。你知道吗

您可以使用:

import keyboard as kb
num = 0
while True:
    if kb.is_pressed('t'):
        num += 1
    print (num)

除此之外,你还有语法和缩进错误。 请参考SO Meta上的这篇文章,这样就不会出现类似的问题。你知道吗

相关问题 更多 >

    热门问题