【发布时间】:2018-12-07 00:31:26
【问题描述】:
import msvcrt
x: int = 0
while not msvcrt.getch() == ' ':
if x <= 10000:
print(x)
x += x
else:
print("space")
按下“空格”时循环不会停止。
【问题讨论】:
标签: python python-3.x windows keypress msvcrt
import msvcrt
x: int = 0
while not msvcrt.getch() == ' ':
if x <= 10000:
print(x)
x += x
else:
print("space")
按下“空格”时循环不会停止。
【问题讨论】:
标签: python python-3.x windows keypress msvcrt
msvcrt.getch() 返回一个字节串而不是一个字符串,因此当您按空格键时,它将返回b' ',而不是' '。
因此改变:
while not msvcrt.getch() == ' ':
收件人:
while not msvcrt.getch() == b' ':
【讨论】:
x: int = 0 更改为x = 0 和x+=x 更改为x+=1。
import msvcrt
x = 0
while not msvcrt.getch() == b' ':
if x <= 10000:
print(x)
x += 1
else:
print("space")
感谢您的关注
【讨论】: