Reputation: 11
so I wanted to create a code in python that automatically type numbers from 1 to 100 and it doesn't work. Hope someone can help
Here's my code
from pynput.keyboard import Key, Controller
import time
keyboard = Controller()
n = 1
time.sleep(2)
while 1 == 1:
keyboard.press(n)
keyboard.release(n)
n = n + 1
(I have "pynput" and "time" installed)
I tried reading error and from what I know have I think it's the problem with this characters "", but if I'm gonna add them I will not be able to add bigger number
Upvotes: 1
Views: 83
Reputation: 40
check your conditions in while loop
from pynput.keyboard import Key, Controller
import time
keyboard = Controller()
n = 1
time.sleep(2)
while n <= 100:
if n <10:
press_key = str(n)
keyboard.press(str(press_key))
keyboard.release(str(press_key))
elif n>=10 and n<=99:
press_key_first_digit = str(n//10)
press_key_second_digit = str(n%10)
keyboard.press(str(press_key_first_digit))
keyboard.release(str(press_key_first_digit))
keyboard.press(str(press_key_second_digit))
keyboard.release(str(press_key_second_digit))
else:
keyboard.press("1")
keyboard.release("1")
keyboard.press("0")
keyboard.release("0")
keyboard.press("0")
keyboard.release("0")
n = n + 1
Upvotes: 0
Reputation: 382
You can convert the type of the variable to string:
while 1 = 1:
for ch in str(n):
keyboard.press(str(ch))
keyboard.release(str(ch))
n = n + 1
But you need to put a for loop because when the number is over 9, you need to send separately all digits for each number.
Upvotes: 2