input() not waiting for user to press enter

536 Views Asked by At

I'm trying to make something that lets you input types of food or restaurants and randomly chooses one of them. However, in the function that should let me add food to the list, the input function isn't waiting for anything to be typed in like it should, and it just says "You need to type in something", around 3 to 4 times before it stops. If I hold the enter key down for long enough while it's doing this, it keeps repeating forever. For some reason, this only happens when I run the main.py file, and when I run it in Pycharm it works fine, aside from os.system('cls') not working.

Here's the code:

    suggestiontype = choosestr(["Restaurant", "Food", "Cancel"], "New..")
    if suggestiontype == 'cancel':
        menu()
    os.system('cls')
    while True:
        suggestionname = input("What's the name of this " + suggestiontype + "?\n" + stuff.color["yellow"] + "> " + stuff.color["end"])
        if suggestionname == '':
            stuff.speak('You need to type in something!')
            os.system('cls')
        else:
            break

And here's the much longer code for choosestr() (used in line 1 of the code above):

def choosestr(choices, text=''):
    def resetarrow():
        choices.remove(color["yellow"] + '^' + color["end"])
        choices.insert(selectedkey + 1 - textspaces, color["yellow"] + '^' + color["end"])
        os.system('cls')
        if textspaces == 1:
            print(color["gray"] + text + color["end"])
        for choice in choices:
            print(choice)

    textspaces = 0
    if text != '':
        textspaces = 1
    selectedkey = 0 + textspaces
    choices.insert(selectedkey + 1 - textspaces, color["yellow"] + '^' + color["end"])
    if textspaces == 1:
        speak('~' + text + '*', .04)
    for choice in choices:
        print(choice)
        time.sleep(.25)
    while True:
        if keyboard.read_key():
            if keyboard.is_pressed("up arrow"):
                if selectedkey > 0 + textspaces:
                    selectedkey -= 1
                    resetarrow()
            elif keyboard.is_pressed("down arrow"):
                if selectedkey < (len(choices)) - 2 + textspaces:
                    selectedkey += 1
                    resetarrow()
            elif keyboard.is_pressed("enter"):
                break

    choices.remove(color["yellow"] + '^' + color["end"])
    os.system('cls')
    choiceword = (choices[selectedkey - textspaces]).lower()
    return choiceword.replace(' ', '_')

There's another function speak() in there, which prints things out character by character, but I don't think that's what's causing the problem.

I thought it might be doing this because it was detecting an enter keypress, so I replaced the first while loop with something like:

while True:
    if keyboard.read_key():
        print(keyboard.read_key())

That didn't print anything unless I actually pressed something, so I think it might have to do with choosestr(). Does anyone know what's going on?

1

There are 1 best solutions below

0
BeRT2me On

My guess is that it has something to do with the \n being used in the input(). Try passing a raw string with r before it instead of just a string.

    while True:
        suggestionname = input(fr"What's the name of this {suggestiontype}?\n{stuff.color['yellow']}> {stuff.color['end']}")
        if suggestionname:
            break
        else:
            stuff.speak('You need to type in something!')
            os.system('cls')