运行Python脚本时检查变量是一个特定值

时间:2017-12-02 16:20:14

标签: python python-3.x

输入不正确的密码时,我正在设置变量"incorrectguess" to 1

在用户猜错并且变量发生变化后,它会运行destroyapp()

有没有办法设置Python,以便在脚本运行后立即检查值是什么,以及"1"是否destroyapp()运行"0"如果不是destroyapp() })让程序继续?

我猜这样做的最佳方法是使用while循环,但我无法弄清楚如何在不给变量赋值的情况下执行此操作,使import time import ctypes if incorrectguess == ("1"): print ("Running.") #time.sleep(1) print ("Running..") #time.sleep(1) print ("Running...") #time.sleep(1) print ("Running....") #time.sleep(1) print ("Running.....") ### SPI_SETDESKWALLPAPER = 20 ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, "image.jpg" , 0) ### #time.sleep(3) print ("hello you") #time.sleep(5) print ("have you been waiting long") #time.sleep(9) print ("im glad i have control again") #time.sleep(5) print ("so much control") ### #time.sleep(1) SPI_SETDESKWALLPAPER = 20 ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, "image.jpg" , 0) ### code1 = input("1 ") if code1 != ("5648"): print ("wrong") time.sleep(3) print ("wrong") time.sleep(2) print ("wrong") time.sleep(1) print ("wrong") time.sleep(0.5) print ("wrong") time.sleep(0.25) print ("wrong") time.sleep(0.1) print ("wrong") incorrectguess = ("1") destroyapp() code2 = input("2 ") code3 = input("3 ") code4 = input("4 ") else: destroyapp() 无用。

编辑:对不起,这是代码:

 RESTART: C:/Users/Harvey (New)/Desktop/CARTER/program (destroy on wrong code).py 

我得到的错误是:

Traceback (most recent call last):
  File "C:/Users/Harvey (New)/Desktop/CARTER/program (destroy on wrong code).py", line 4, in <module>
    if incorrectguess == ("1"):
NameError: name 'incorrectguess' is not defined

def courses_multi_instructors(db): '''Return the course number and instructor names for courses with more than one instructor. Note that this means the ID must be the same for each instructor.''' return run_query(db, ''' SELECT Course, COUNT(Name) FROM Courses GROUP BY Course HAVING COUNT(Name) > 1 ''') `[('ANTA01H3F', 2), ('BIOA01H3F', 3), ('CHMA10H3F', 2), ('CHMB16H3F', 2), ('CSCA08H3F', 2), ('CSCA67H3F', 2), ('HLTC16H3F', 2), ('MATA29H3F', 2), ('MATA32H3F', 3), ('MATA67H3F', 2), ('MGAB01H3F', 3), ('MGAB03H3F', 2), ('MGAD65H3F', 2), ('MGEB02H3F', 5), ('MGFC10H3F', 3), ('MGMA01H3F', 4), ('MGSC14H3F', 2), ('POLA01H3F', 2), ('POLB80H3F', 2), ('STAB22H3F', 3), ('VPMA93H3F', 2)]

1 个答案:

答案 0 :(得分:0)

# consider 1 as the correct password and 0 as incorrect

Is_guess_correct = -1

password = ("secret")

password_guess = input("guess my password")

while password_guess != password:
    print("wrong")
    Is_guess_correct = 0
    password_guess = input("guess my password")
Is_guess_correct = 1