1

This is a code snippet for python mode of Processing. The purpose of this code is to update a variable when a key is pressed. Processing has a variable called keyPressed which is True if a key is pressed. The identity of the key that is pressed is saved as a string in the variable named key.

 if keyPressed:
         if key == "a":
            a += 0.1
        elif key == "s":
            a -= 0.1
        elif key == "q":
            q += 1
        elif key == "w":
            q -= 1
        elif key == "z":
            z += 3
        elif key == "x":
            z -= 3

Is this just the best way to write this, or is there a better way? If not, what if there were 100 keys each with a different variable to tweak?

John Salter
  • 182
  • 10
  • You could use a switch-case statement, for many items it is faster than elif statements https://stackoverflow.com/questions/767821/is-else-if-faster-than-switch-case – herisson Jul 27 '20 at 21:48
  • 1
    @Antoine Stock, an example is about C language. – ipj Jul 27 '20 at 21:50
  • Oh that's right nevermind, i used too much C# recently – herisson Jul 27 '20 at 21:58
  • A switch case has been requested for Python, but never implemented. https://www.python.org/dev/peps/pep-3103/ – ipj Jul 27 '20 at 22:02
  • 1
    This would be easier if the variables "a", etc... were keys in a dictionary or members of a class instance. Are these variables global to the module? – tdelaney Jul 27 '20 at 22:07
  • @MrNobody33 - I don't see how that solves the problem of optinally assigning values to the variables a, q and z. – tdelaney Jul 27 '20 at 22:15

2 Answers2

3

The challenge is that the if's are deciding which variable to update along with the value to add to the variable. Global variables can by accessed dynamically by name through the global() namespace call. I noticed that one key updates a like-named variable and then the next key decrements that same variable by the same amount. So, that's 3 pieces of information per variable. You could put this into a list (or tuples) and then build a dictionary that tells you what action to take on each key press.

# var key, negated var key, value triplets
key_db = (("a", "s", .1), ("q", "w", 1), ("z", "x", 3))
key_index = {}
for var, nvar, val in key_db:
    key_index[var] = (var, val)
    key_index[nvar] = (var, -val)
    
if key_pressed:
    try:
        var, val = key_index[key]
        globals()[var] = globals().get(var, 0) + val
    except KeyError:
        pass
tdelaney
  • 73,364
  • 6
  • 83
  • 116
0

There would be no way around using the first if statement but you can replace the second if statement with a dict.

And to differentiate adding and subtracting just negate the number.

ops = {'a': (a, 0.1),
       's': (a, -0.1),
       'q': (q, 1),
       'w': (q, -1),
       'z': (z, 3),
       'x': (z, -3)}

if keyPressed:
    var, amount = ops[key]
    var += amount
Jab
  • 26,853
  • 21
  • 75
  • 114
  • 2
    There are variables `a,q,z` to change not only `a`. Do You change it or I'm something missed? – ipj Jul 27 '20 at 21:54
  • `ops` cannot be defined: `NameError: name 'a' is not defined`. Should we define all 100 keys by hand? – ipj Jul 27 '20 at 22:04
  • @ipj - how else would you define them? You are going to have to at some point. Using a dictionary for this use case is a good solution. – wwii Jul 27 '20 at 22:09
  • I tried this but, while it gave no errors, the buttons did nothing when pressed. I thought this may be something to do with Processing. I tested it in python directly by replacing keyPressed with 1 ==1, and setting up a variable called key which I assigned to the string "a". I did not find that running the if statement caused a to increase by 0.1. (I am a novice, i may have made a mistake.). If you want to see my code, please use below link https://pastebin.com/cxwiD2QU – John Salter Jul 27 '20 at 22:12
  • 2
    I've tested the code and values of variables `a,q,z` remain unchanged. Only `var` is changed, but it is temporary variable which is overritten when next key is pressed. – ipj Jul 27 '20 at 22:16