【问题标题】:python wait nth digit before continuepython在继续之前等待第n个数字
【发布时间】:2017-12-18 09:02:31
【问题描述】:
from pad4pi import rpi_gpio

# Setup Keypad
KEYPAD = [
        ["1","2","3","A"],
        ["4","5","6","B"],
        ["7","8","9","C"],
        ["*","0","#","D"]
]

ROW_PINS = [5,6,13,19] # BCM numbering
COL_PINS = [26,16,20,21] # BCM numbering

factory = rpi_gpio.KeypadFactory()

keypad = factory.create_keypad(keypad=KEYPAD, row_pins=ROW_PINS, col_pins=COL_PINS)

def processKey(key):
 print("enter 3 digit")
 print(key)
 if key == 123:
  print("correct")
 else:
  print("wrong password")

keypad.registerKeyPressHandler(processKey)

我希望代码等待用户输入例如 3 位数字,然后再与上面代码中的 123 代码中的密码进行比较。

应该怎么做:

等待用户从键盘输入 3 位数字,例如 123,然后打印正确。

实际作用:

用户输入1位密码后会立即打印正确或错误的密码

【问题讨论】:

  • 检查if len(key) == 3: 但你必须将key 保留在全局值中并附加新字符。

标签: python python-2.7


【解决方案1】:

以@furas 为例更新覆盆子:

# Initial keypad setup
code = ''

def processKey(key):
    print("Enter your 3 digit PWD: \n")
    global code
    MAX_ALLOWED_CHAR = 3
    code += key
    if (len(code) == MAX_ALLOWED_CHAR):
        if (code == "123"): 
            print("You entered the correct code.")
            dostuff()
        else:
            code = ''        
            print("The passcode you entered is wrong, retry.")

def dostuff():
    # do your things here since passcode is correct.

这可能适合您的情况。


def processKey():

    key = input("enter 3 digit")
    if (key == "123"):
        print("Correct password.")
        return True
    else:
        print("You typed {0} wich is incorrect.".format(key))
        return False

所以现在你不给 processKey 一个值,因为正如你所说的用户输入它,调用 processKey() 将要求用户输入密码并根据检查中的“123”返回真/假。

这是如果您想输入密码,但如果以下答案不适合您的需求(没有完全理解您要完成的任务),请提供更聪明的示例。

编辑:

由于您希望严格输入 3 位数字并重新输入密码,以防输入错误,您可以执行以下操作:

在调用 processKey() 时,您可以:

while (processKey() == False):
    processKey()

修改后的代码以满足您的需求:

def processKey():
    MAX_ALLOWED_CHAR = 3
    key = input("Enter 3 digit PWD: \n")
    if (key == 123):
        print("Correct password.")
        return True
    elif (len(str(key)) > MAX_ALLOWED_CHAR):
        print("The max allowed character is {0}, instead you entered {1}.".format(MAX_ALLOWED_CHAR,key))
        return False
    else:
        print("You typed {0} wich is incorrect.".format(key))
        return False

while (processKey() == False):
    processKey()

输出:

Enter 3 digit PWD: 
3333
The max allowed character is 3, instead you entered 3333.
Enter 3 digit PWD: 
321
You typed 321 wich is incorrect.
Enter 3 digit PWD: 
123
Correct password.

【讨论】:

  • 我希望 python 程序从键盘获取三位数密码。
  • 感谢您的回答。这适用于普通键盘,但我在树莓派上使用 4x4 矩阵键盘。
  • 再看一遍,代码更新了。抱歉,我没有看到你指定覆盆子的东西
【解决方案2】:

keypress 在每次按键后执行 - 这是很自然的。您必须将所有键保留在列表或字符串中并检查其长度。

code = ''

def processKey(key):
    global code

    code += key

    if len(code) == 3:
        if code == "123": 
           print("correct")
        else:
           print("wrong password, try again")
           code = ''

【讨论】:

  • 如果密码错误,如何编程回询问密码状态?
猜你喜欢
  • 2018-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-14
  • 2019-01-20
  • 2015-12-22
相关资源
最近更新 更多