Search code examples
pythongetch

How to avoid problem with function getch()


I've got a problem with my program. It's kind of looped menu. I write in VS Code. The problem is when I run program and try to choose one of the three options it runs option after else: print("No more option"). The code below:

from getch import getch
def func_1():
    print("Hello")
def name():
    name = input("What is your name?: ")
    print("Your name is: "+name)
def do_sth(a=3):
    return 2 * a
while True:
    print("1) Wyświetl wynik funkcji")
    print("2) Wyświetl imię")
    print("3) Wyświetl do Sth")
    keyPressed=getch()
    if keyPressed =='1':
        func_1()
    elif keyPressed == '2':
        name()
    elif keyPressed =='3':
        print(do_sth())
        press = input("Press any key to continue....")
    else:
        print("No more option")

But when I wrorte the same code on my Android smatphone in Pydroid 3 it works everything fine it runs every function separatelly. I don't know why is that? I also wrote the code above in PyCharm Community and it doesn't read any key. But in Pydroid 3 on my android smatphone works code perfectlly.


Solution

  • The issue is comparing a bytestring with a string.

    Try:

    from getch import getch
    
    def func_1():
        print("Hello")
    
    def name():
        name = input("What is your name?: ")
        print("Your name is: "+name)
    
    def do_sth(a=3):
        return 2 * a
    
    while True:
        print("1) Wyświetl wynik funkcji")
        print("2) Wyświetl imię")
        print("3) Wyświetl do Sth")
        keyPressed=getch()
        if keyPressed == b'1':
            func_1()
        elif keyPressed == b'2':
            name()
        elif keyPressed == b'3':
            print(do_sth())
            press = input("Press any key to continue....")
        else:
            print("No more option")