【问题标题】:How to avoid problem with function getch()如何避免函数 getch() 出现问题
【发布时间】:2020-08-07 09:12:03
【问题描述】:

我的程序有问题。这是一种循环菜单。我用 VS Code 编写。问题是当我运行程序并尝试在 else 之后选择它运行的三个选项之一: print("No more option").下面的代码:

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")

但是,当我在 Pydroid 3 中的 Android smatphone 上编写相同的代码时,它工作得很好,它可以单独运行每个功能。我不知道这是为什么?我还在 PyCharm 社区中编写了上面的代码,它没有读取任何密钥。但在我的 android smatphone 上的 Pydroid 3 中,代码完美。

【问题讨论】:

  • 虽然我看到不同的帖子在我的 vs 代码中不起作用
  • 我在您的代码中没有看到print('Error')
  • 确定没有更多选项
  • keypressed = getch() : print("[%s]" % keypressed) 之后尝试这样做,然后发布你得到的结果。
  • 尝试将 b 添加到 "1".. 即 if keypressed == b"1": 并使用 2 和 3 重复此操作

标签: python getch


【解决方案1】:

问题是将字节串与字符串进行比较。

试试:

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")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多