Python密码警报五分钟

问题描述

我已经为游戏编写了这个python程序:

print("CC ACTIVATED")
import lcddriver
import time
import subprocess
display = lcddriver.lcd()
try:
    display.lcd_display_string("CC... ",1) 
    time.sleep(2)
    display.lcd_display_string("ONLINE",2)
    time.sleep(2)
    display.lcd_clear()
except Exception:
    print("SCREEN ERROR")
try:
    display.lcd_display_string("SETUP A",1) 
    display.lcd_display_string("PASSWORD? Y/N",2)
except Exception:
    print("SCREEN ERROR")
activate = input("")
if activate == 'y':
    print("ACTIVATED")
    try:
        display.lcd_clear()
        display.lcd_display_string("",1) 
        time.sleep(2)
        display.lcd_display_string("LOADING",2)
        time.sleep(2)
        display.lcd_clear()
    except Exception:
        print("SCREEN ERROR")

else:
    print("ABORT")
    try:
        display.lcd_clear()
        display.lcd_display_string("",1) 
        time.sleep(2)
        display.lcd_display_string("ABORT",2)
        time.sleep(2)
        display.lcd_clear()
        subprocess.call(["sudo","halt"])
    except Exception:
        print("SCREEN ERROR")
        subprocess.call(["sudo","halt"])
k = True
while k:
    
    try:
        display.lcd_clear()
        display.lcd_display_string("ENTER PASWORD",1) 
        display.lcd_display_string("----------------",2)
    except Exception:
        print("SCREEN ERROR")
    pasword = input("")
    display.lcd_clear()
    try:
        display.lcd_clear()
        display.lcd_display_string("YOU TYPED:",1) 
        display.lcd_display_string(pasword,2)
        time.sleep(2)
        display.lcd_display_string("CONFIRM? Y/N",1) 
    except Exception:
        print("SCREEN ERROR")
    ok = input("")
    if ok == 'y':
        k = False
    else:
        display.lcd_clear()
try:
    display.lcd_clear()
    display.lcd_display_string("PASSWORD",1) 
    display.lcd_display_string("SET",2)
except Exception:
    print("SCREEN ERROR")
time.sleep(2)
run = True
try:
    display.lcd_clear()
    display.lcd_display_string("STARTING ",1) 
    display.lcd_display_string("GAME...",2)
except Exception:
    print("SCREEN ERROR")
time.sleep(2)
while run:
    try:
        display.lcd_clear()
        display.lcd_display_string("ENTER PASSWORD ",1) 
        display.lcd_display_string("TO DEACTIVATE",2)
    except Exception:
        print("SCREEN ERROR")
    pasword1 = input("")
    if pasword1 == pasword:
        try:
            display.lcd_clear()
            display.lcd_display_string("PASSWORD....",1)
            time.sleep(2)
            display.lcd_display_string("ACCEPTED",2)
            time.sleep(2)
            display.lcd_clear()
            display.lcd_display_string("DEACTIVATED",2)
            subprocess.call(["sudo","halt"])
            time.sleep(10)
        except Exception:
            print("SCREEN ERROR")
            subprocess.call(["sudo","halt"])
    else:
        try:
            display.lcd_clear()
            display.lcd_display_string("PASSWORD....",1)
            time.sleep(2)
            display.lcd_display_string("UNACCEPTED",2)
            time.sleep(2)
        except Exception:
            print("SCREEN ERROR")

它可以在树莓派上运行,并且绝对可以正常工作,但是我想做的一件事比我的技能水平高一点。我需要该程序为您提供三次尝试猜测密码的机会,然后在第四次尝试播放音频录音时使用。之后,它需要用录音来响应失败的尝试五分钟,然后再回到无声的三个机会。我知道如何进行录音,但五分钟的时间需要帮助。这里有人知道怎么做吗?谢谢。

解决方法

要允许进行三次密码尝试,您将需要一个计数器变量,每次尝试失败时,计数器变量最多可以计数三次。

您不仅要同时检查k == Truek == True,还要检查attempts <= 3。只要等待五分钟,您就需要为用户最后一次猜测的时间创建一个时间戳。

from datetime import datetime

...
k = True
password_attempts = 0

while k and password_attempts < 4:
    try:
        display.lcd_clear()
        display.lcd_display_string("ENTER PASWORD",1)
        display.lcd_display_string("----------------",2)
    except Exception:
        print("SCREEN ERROR")
    pasword = input("")
    display.lcd_clear()
    try:
        display.lcd_clear()
        display.lcd_display_string("YOU TYPED:",1)
        display.lcd_display_string(pasword,2)
        time.sleep(2)
        display.lcd_display_string("CONFIRM? Y/N",1)
    except Exception:
        print("SCREEN ERROR")
    ok = input("")
    if ok == "y":
        k = False
    else:
        display.lcd_clear()
        password_attempts += 1
        if password_attempts == 4:
            #Create a timestamp of last attempt
            last_request = datetime.now()
            current_request = datetime.now()
            while((last_request - current_request).total_seconds() < 5 * 60):
                #Code to play music
                current_request = datetime.now()

然后由您决定将播放器重新路由到程序中再次尝试输入密码的位置。

但是,您在该项目开始时所做的设计选择将真正开始影响您想要做的事情。

我建议您考虑对程序进行重新设计,以使编写程序更轻松,更愉快。

编程的最好部分之一是代码重用;通过函数最容易理解这一点。在Python中,您可以使用语法def function_name():

创建一个函数

然后,当您想在该函数中运行代码时,只需按名称function_name()来调用它即可。

这对您的应用意味着什么?

您可以使用一种方法将写入LCD换行,从而避免重复键入相同的try:... except:块(顺便说一句,当您写入except Exception时,您将捕获所有您的程序可能引发的异常,这被认为是不好的做法,因为您可能不希望执行相同的操作,而无论异常最初是什么,而是尝试找到要捕获的特定异常,然后再捕获具体来说就是那个)。

重写LCD打印的示例:

def print_to_lcd(statements_to_print: [str]):
    line_number = 1
    try:
        for statement in statements_to_print:
            display.lcd_display_string(statement,line_number)
            line_number += 1
    except Exception:
        print("SCREEN ERROR")

然后,您只需生成要打印的LCD列表,然后使用它们来调用LCD函数:

things_to_write = ["PASSWORD...","UNACCEPTED"]
print_to_lcd(things_to_write)

关于如何检查密码值,播放音乐等时也可以这样说。

每当您发现自己反复重写相同的信息时,请花点时间思考一下是否有办法将其写入一次,然后重新使用它。这将使您的程序更容易编写,也更易于使用。

在这种情况下尤其如此,因为您要在等待一段时间后重新执行相同的操作。想象一下,而不是必须编写大量的if语句,而是当用户第四次输入密码错误时,您只需编写如下内容:

play_music_for_five_minutes()
prompt_for_password()

让我知道是否还有其他问题可以回答!

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...