我正在尝试制作一个简单的python脚本来诱骗我的朋友认为得到病毒除非他写密码p3nis47但是每当我尝试运行它时我会在第1,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()
答案 0 :(得分:2)
使用输入错误运行程序会出错
$scope.Ctrl.type = "" + $scope.Ctrl.type + "";
那是因为你正在使用全局变量,但是你需要让python知道它。将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
添加到函数的开头。然后还有另一个错误
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
所以,我运行了修复程序......但现在我已经感染了病毒!