1

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?

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Evan S
  • 11
  • 3
  • Where you have `?\n` try `?\\n` instead. – BeRT2me Apr 10 '22 at 22:27
  • Print your stuffs with `print` and just use input without prompts. In addition: later you use "up and down keys", so you may get terminal in a different mode, so input may not work as expected in such "raw"/"direct" mode. Also you seems to mix colours and the `os.system('cls')` is ugly. If you need to control terminals (and colours) use `curses` a standard module. Do no mix terminal control, it will create chaos, and it may work only on your machine. Mixing`stuff.colour` and `colour` (and the `cls`) really smell bad. Avoid such bad habits. – Giacomo Catenazzi Apr 11 '22 at 06:27

1 Answers1

0

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')
BeRT2me
  • 12,699
  • 2
  • 13
  • 31