我正在尝试制作一个简单的python脚本来欺骗我的朋友认为get会感染病毒,除非他写下密码p3nis47,但是每当我尝试运行它时,我都会在第17,4和9行出现错误。 17 和 4 只是方法,我不知道为什么我会收到错误,在 9 时我只是减去 1 以确保我只给我的朋友3 尝试输入"密码"。对不起,如果这真的很明显,我刚刚开始学习 python。
count = 4
def ask():
answer = input("do you wan't a virus ")
respond(answer)
def respond(response):
if(response == "p3nis47"):
print("congrats!!! you don't have a virus")
else:
count = count - 1
if(count == 0):
print("trololololololololololol")
print(answer,"is not a vailid answer")
print("you have ",count," attempts remaining")
ask()
ask()
使用错误的输入运行程序会产生错误
Traceback (most recent call last):
File "c.py", line 15, in <module>
ask()
File "c.py", line 4, in ask
respond(answer)
File "c.py", line 9, in respond
count = count - 1
UnboundLocalError: local variable 'count' referenced before assignment
这是因为您使用的是全局变量,但您需要让 python 知道它。将global count
添加到函数的开头。然后还有另一个错误
Traceback (most recent call last):
File "c.py", line 16, in <module>
ask()
File "c.py", line 4, in ask
respond(answer)
File "c.py", line 13, in respond
print(answer,"is not a vailid answer")
NameError: name 'answer' is not defined
这也很简单。您只是不小心使用了错误的变量名称。有两个更改的工作脚本是
count = 4
def ask():
answer = input("do you wan't a virus ")
respond(answer)
def respond(response):
global count
if(response == "p3nis47"):
print("congrats!!! you don't have a virus")
else:
count = count - 1
if(count == 0):
print("trololololololololololol")
print(response,"is not a vailid answer")
print("you have ",count," attempts remaining")
ask()
ask()
所以,我运行了修复程序...但现在我感染了病毒!