-2

I don't really know how word the question but I want the "h" or "j" or "k" inputs to display their respective values

from pynput.keyboard import Key, Controller
import time
import random

keyboard = Controller()

wasd = ["w","a","s","d","j","h","k"]

for x in range(1,11):
    if wasd[random.randint(0,6)] == "w":
        keyboard.press("w")
        time.sleep(0.5)
        keyboard.release("w")
    elif wasd[random.randint(0,6)] == "a":
        keyboard.press("a")
        time.sleep(1)
        keyboard.release("a")
    elif wasd[random.randint(0,6)] == "s":
        keyboard.press("s")
        time.sleep(0.1)
        keyboard.release("s")
    elif wasd[random.randint(0,6)] == "d":
        keyboard.press("d")
        time.sleep(1)
        keyboard.release("d")
    elif wasd[random.randint(0,6)] == "h" or "j" or "k":
        keyboard.press("h" or "j" or "k")
        time.sleep(0.03)
        keyboard.release("h" or "j" or "k") 

here is the code and this is the output: shdhswhhhh

so whenever its either h, j or k it only types h

1 Answers1

0

In the code you provided, the following line is incorrect:

elif wasd[random.randint(0,6)] == "h" or "j" or "k":

This line of code is always True because "j" and "k" are non-empty strings that are considered True in Python. Therefore, the code block under this condition is executed every time and it always types "h".

To fix this issue, you can change the line to:

elif wasd[random.randint(0,6)] in ["h", "j", "k"]:

This code will check if the random element from wasd. If it is, the code block under this condition will be executed and the corresponding character will be typed.

Also, keyboard.press("h" or "j" or "k") will always press "h".

So you can change it to


keyboard.press(random.choice(["h", "j", "k"]))


OP's comment " if the result from wasd[random.randint(0,6)] in ["h", "j", "k"] was 'k', then would it not re-choose another letter from the keyboard.press(random.choice(["h", "j", "k"])), making the first choice redundant? "

For this just save your random choice value in an variable like this

for x in range(1,11):
    randomChoice = wasd[random.randint(0,6)]
    if randomChoice == "w":
        keyboard.press("w")
        time.sleep(0.5)
        keyboard.release("w")
    elif randomChoice == "a":
        keyboard.press("a")
        time.sleep(1)
        keyboard.release("a")
    elif randomChoice == "s":
        keyboard.press("s")
        time.sleep(0.1)
        keyboard.release("s")
    elif randomChoice == "d":
        keyboard.press("d")
        time.sleep(1)
        keyboard.release("d")
    elif randomChoice == "h" or "j" or "k":
        keyboard.press(randomChoice)
        time.sleep(0.03)
        keyboard.release(randomChoice) 

Q2. what is the random.choice thing for?

Ans. It is a function that chooses a random value from a given sequence.

codester_09
  • 5,622
  • 2
  • 5
  • 27
  • sorry for the stupid question but what is the `random.choice` thing for? and also, wouldn't it reshuffle the choice, for example, if the result from `wasd[random.randint(0,6)] in ["h", "j", "k"]` was 'k', then would it not re-choose another letter from the `keyboard.press(random.choice(["h", "j", "k"]))`, making the first choice redundant? – LukeWantsToAskAQuestion Feb 18 '23 at 20:37