0
import time
import keyboard

Commands = []
Command = {}
RawCommand = ""
repeat = 0

while True:
    Command.update({ "RawCommand": input("Command? ")})
    Command.update({ "Times": int(input("How many times? (NUMBER!!) "))})

    if Command["RawCommand"] == "Done":
        Command.clear()
        for LoopCommand in Commands:
            RawCommand = LoopCommand["RawCommand"]
            while LoopCommand["Times"] != 0:
                if RawCommand == "Walk":
                    keyboard.press("W")
                    time.sleep(0.25)
                    keyboard.release('W')
                if RawCommand == "Jump":
                    keyboard.press(" ")
                    time.sleep(0.04)
                    keyboard.release(' ')
                LoopCommand["Times"] = LoopCommand["Times"] - 1
        Commands.clear()
    else:
        Commands.append(Command)
        print(Commands)

The code is meant to take commands I give it and run them for a game. However, the for loop that's meant to comprehend and run commands just uses the "Done" command used to initiate the running of commands instead of the actual table of commands.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • 1
    Normally in Python variables are all `lower_case` to distinguish them from classes and such that are often `UpperCase` style. – tadman Aug 10 '23 at 00:43
  • 1
    Using `update` here is a kind of clumsy way of doing `Command["RawCommand"] = ...` Note when you `append` you may end up appending the same object over and over, cross-linking your array. I'd create a new object per loop. – tadman Aug 10 '23 at 00:44
  • You may want to think about slimming down your design here, using shorter, yet still meaningful names. For example: `command["raw"]` or just treating command as a tuple/list instead, then you can do `for command, times in commands:` easily. Python can easily get super cluttered if you're not careful to keep it under control. Here you have a "chain of `if` statements" instead of a data-driven look-up table with command functions in it, something that would be *way* cleaner and easier to debug. – tadman Aug 10 '23 at 00:46
  • 1
    Consider: `COMMAND = { "walk": walk, "jump": jump }` where `walk` and `jump` are simple functions that can be *tested independently* of the main body. – tadman Aug 10 '23 at 00:49
  • `Commands.append(Command)` -- `Command` is the same dict object every time. Hope that explains why this question is effectively a duplicate. BTW, welcome to Stack Overflow! Check out the [tour], and [ask] if you want tips. One important thing is to make a [mre] in order to isolate the problem and help others understand it. You might also want to learn [How to step through your code to help debug issues](/q/4929251/4518341). – wjandrea Aug 10 '23 at 00:53
  • That said, using `Command.copy()` would be a bit of a clumsy solution. It'd be simpler to just make a new one every loop, like for example `raw = input(...); times = input(...); command = {'raw': raw, 'times': times}`. – wjandrea Aug 10 '23 at 00:58
  • Okay the suggestions I have been given may be working except now its telling me raw doesnt exist even though I changed every RawCommand with raw. – Zane Sites Aug 10 '23 at 01:10

0 Answers0