Including punctuation in a string response?

Hello everyone,

I am trying to design a linguistic experiment that participants need to form a sentence from given words. I have been writing a code, for the string input, but I came across several problems that I could not figure out. I want to include some punctuation in the string input (so far I have the period, apostrophe, the spacebar and comma), but I could not find the way to include some special punctuation such as the colon, exclamation point, parentheses and so on. I have been looking some forums talking about string text too, but it did help out. Can someone tell me what is wrong with the code? I appreciate your help a lot!

elif theseKeys[i]=='backspace':
    inputText=inputText[:-1]  #lose the final character
    i = i + 1
elif theseKeys[i] =='space':
    inputText += ' '
    i = i + 1
elif theseKeys[i] in ['lshift', 'rshift']:
    shift_flag = True
    i = i + 1
elif theseKeys[i] == 'period':
    inputText = inputText +"."
    i = i + 1
elif theseKeys[i] == 'apostrophe':
    inputText = inputText + "'"
    i = i + 1
elif theseKeys [i] == 'comma':
    inputText = inputText + ","
    i = i + 1
elif theseKeys[i] == 'colon':
    inputText = inputText + ":"
    i = i + 1
elif theseKeys[i] == 'parentheses':
    inputText = inputText + "()"
    i = i + 1
elif theseKeys[i] == 'exclamation point':
    inputText = inputText + "!"
    i = i + 1

else:
    if len(theseKeys[i]) == 1:
        #we only have 1 char so should be a normal key,
        #otherwise it might be 'ctrl' or similar so ignore it
        if shift_flag:
            inputText += chr( ord(theseKeys[i]) - ord(' '))
            shift_flag = False
        else:
            inputText += theseKeys[i]

    i = i + 1

Switch to the Coder view and go to the Demos menu. Under Input, select what_key.py and run it.

This demo will print out the name corresponding to any key you press (it stops after 5, so just run it again if needed).

Note that some characters require a sequence of keys to be pressed. i.e. you can’t type 'colon' directly, without first pressing a shift key.

Hi Michael,

Thank you very much for your help!
I rewrote the code, and it worked out pretty well, except for one problem that when I hit “9” on the keyboard, even without a code, it printed as “(”. I have been playing with the syntax to create a single combination like [(‘lshift’,‘9’) and (rshift’,‘9’)] but it does not work, too. If you know the problem with my code, please let me know, I appreciate your help a lot!.

Here is a part of the rewrote codes:

elif theseKeys[i] in ['lshift','semicolon','rshift']:
    inputText = inputText + ":"
    shift_flag = True
    i = i + 1
elif theseKeys[i] in ['lshift','rshift','9']:
    inputText += '('
    shift_flag = True
    i = i + 1
elif theseKeys[i] in ['lshift','rshift','0']:
    inputText += ')'
    shift_flag = True
    i = i + 1
elif theseKeys[i] in ['lshift','rshift','1']:
    inputText += '!'
    shift_flag = True
    i = i + 1
1 Like

You need to think like a computer. Imagine a single key is pressed (the one indexed as theseKeys[i]). Then work your way through every comparison in your code, in order. Do you see how you respond to shift keys multiple times? Only the first time is correct or needed. When a shift key is typed, you don’t want to add to your stored text, but just note down that the shift key is currently pressed.

In essence, things should look more like this:

elif  shift_flag and theseKeys[i] == 'semicolon':
    inputText += ')'
    i = i + 1

but I’d need to see the entirety of your code to suggest how to handle what the non-shifted character.

1 Like

Thank you Michael! I figured out what was wrong.
Thank you so much for your help!