Python 3 for循环重复2次



我在python脚本中遇到了一个问题,for循环有时会重复2次。比如它应该每个字母写一次,有时(我看到它大部分是在' '之前)它写两次。

from pynput.keyboard import Key, Controller
import time
partition = '35 66 67 88 89 77 655 6  35 66 67 88 89 77 65 6  35 66 67 88 90 4 4 090 6  67 889 0 6 68 77 86 7 67 889 0 6 68 77 86 7 0 4 000 09  9 8 787 6 9 8 787 6 67 8 90 987 890 9  89 0 98 787 66 75 6  67 8 78 989 098 6  67 890 4 69 8 7 0 4 000 09  9 8 787 6'
enter code here
note = ''
temps = 0.1
keyboard = Controller()
print('Starting')
for i in range(3, 0, -1):
print(i)
time.sleep(1)
print('Go')
for i in partition:
while i == '0' or i == '1' or i == '2' or i == '3' or i == '5' or i == '6' or i == '7' or i == '8' or i == '9':
if i == '1':
note = '&'
if i == '2':
note = 'é'
if i == '3':
note = '"'
if i == '5':
note = "("
if i == '6':
note = '-'
if i == '7':
note = 'è'
if i == '8':
note = '_'
if i == '9':
note = 'ç'
if i == '0':
note = 'à'
keyboard.press(note)
time.sleep(0.1)
keyboard.release(note)
time.sleep(temps)
break
while i == '4':
note = "'"
keyboard.press(Key.shift_l)
keyboard.press(note)
time.sleep(0.1)
keyboard.release(note)
keyboard.release(Key.shift_l)
time.sleep(temps)
break
while i == ' ':
time.sleep(0.04)
break
print(note)
print('Réussi')

(很多变量都是用法语写的,但这并不重要)代码用于在钢琴上播放游戏《Raft》中的一首歌(游戏邦注:上面的一系列数字),所以我必须将每个数字分配给键盘的一个键。你可以在你的计算机上使用代码,因为按下的键是在终端上打印的(注)。我的问题是它不能正确播放,有时重复相同的键。

那你能帮我让它正常工作吗?

代码有点乱,所以提前抱歉。(我两周前开始学习python)

非常感谢那些会帮助我的人!

它不按任何按钮两次你只是没有改变的值的NOTE值分区为";所以(因为每次for循环运行…如果你让它打印笔记,它会第二次打印上一个笔记的值。

我在我加的那行加了2 #。删除它们以使其工作

from pynput.keyboard import Key, Controller
import time
partition = '35 66 67 88 89 77 655 6  35 66 67 88 89 77 65 6  35 66 67 88 90 4 4 090 6  67 889 0 6 68 77 86 7 67 889 0 6 68 77 86 7 0 4 000 09  9 8 787 6 9 8 787 6 67 8 90 987 890 9  89 0 98 787 66 75 6  67 8 78 989 098 6  67 890 4 69 8 7 0 4 000 09  9 8 787 6'
enter code here
note = ''
temps = 0.1
keyboard = Controller()
print('Starting')
for i in range(3, 0, -1):
print(i)
time.sleep(1)
print('Go')
for i in partition:
while i == '0' or i == '1' or i == '2' or i == '3' or i == '5' or i == '6' or i == '7' or i == '8' or i == '9':
if i == '1':
note = '&'
if i == '2':
note = 'é'
if i == '3':
note = '"'
if i == '5':
note = "("
if i == '6':
note = '-'
if i == '7':
note = 'è'
if i == '8':
note = '_'
if i == '9':
note = 'ç'
if i == '0':
note = 'à'
keyboard.press(note)
time.sleep(0.1)
keyboard.release(note)
time.sleep(temps)
break
while i == '4':
note = "'"
keyboard.press(Key.shift_l)
keyboard.press(note)
time.sleep(0.1)
keyboard.release(note)
keyboard.release(Key.shift_l)
time.sleep(temps)
break
while i == ' ':
time.sleep(0.04)
#note = " "#
break
print(note)
print('Réussi')

最新更新