擅长:python、mysql、java
<p>您需要将条件<code>counter==3</code>移动到while循环中</p>
<p>也可以这样做</p>
<pre><code>import sys
password = input("Enter password : ")
for __ in range(2): # loop thrice
if (password=="Test"):
break #user has enterd correct password so break
password = input("Incorrect, try again : ")
else:
print ("Locked out")
sys.exit(1)
#You can put your normal code that is supposed to be
# executed after the correct password is entered
print ("Correct password is entered :)")
#Do whatever you want here
</code></pre>
<p>一个更好的方法是将这个密码检查打包到一个函数中。在</p>
^{pr2}$