print "Password?"
main = 1.0
tries = 1
Pass = raw_input()
if Pass == "hi": print "Granted access."
elif Pass == "swag": print "really?"
else:
print "Wrong, try again."
print "Wrong", tries, "try."
tries = tries + 1
print "Password?"
Pass = raw_input()
if Pass == "hi": print "Granted access."
else:
print "Wrong,", tries, """tries.
try again?""" #test # line 18
tries = tries + 1
answer = raw_input()
if answer == "Yes": print "Password?"
Pass = raw_input() # line 22
if Pass == "hi": print "WOW SUCH PASS"
else:
print "Wrong,", tries, "tries...aborting"
tries = tries + 1
else: # line 27
print "Okay."
检查此代码。
我正在第18行尝试询问用户是否想再次尝试接听密码。 如果是的话,请求密码并检查我的“通行证”。 如果用户不想回答密码,则应将其重定向到代码行27。
但已经在第22行出现了问题。
我已经尝试了几种方法来格式化这段代码,而且我很新。有人想帮忙解决这个问题吗?
编辑:对于误导性的标题感到抱歉,我们不确定该怎么做。
答案 0 :(得分:0)
你必须让你的缩进正确。第21行和第22行似乎是奇数,以及24上的挂起else
。您可能希望暂时避免内联if cond: pass
构造,直到您掌握Python的缩进。
我可能会也可能没有正确地对您的块进行分组,但是这里的代码应该看起来:
if answer == "Yes":
print "Password?"
Pass = raw_input()
if Pass == "hi":
print "WOW SUCH PASS"
else:
print "Wrong,", tries, "tries...aborting"
tries = tries + 1
else:
print "Okay."
答案 1 :(得分:0)
评论扩展:python要求您正确缩进代码。每个街区有4个空间。有关要求列表,请参阅http://www.python.org/dev/peps/pep-0008/。
print "Password?"
main = 1.0
tries = 1
Pass = raw_input()
if Pass == "hi":
print "Granted access."
elif Pass == "swag":
print "really?"
else:
print "Wrong, try again."
print "Wrong", tries, "try."
tries = tries + 1
print "Password?"
Pass = raw_input()
if Pass == "hi":
print "Granted access."
else:
print "Wrong,", tries, """tries.
try again?""" # test # line 18
tries = tries + 1
answer = raw_input()
if answer == "Yes":
print "Password?"
Pass = raw_input() # line 22
if Pass == "hi":
print "WOW SUCH PASS"
else:
print "Wrong,", tries, "tries...aborting"
tries = tries + 1
else: # line 27
print "Okay."
答案 2 :(得分:0)
我认为这是代码的正确缩进版本。它在语法方面是正确的,但可能不是在行为方面 - 这是你要调查的。您应该阅读注释中建议的Python教程和http://www.python.org/dev/peps/pep-0008/。
print "Password?"
main = 1.0
tries = 1
Pass = raw_input()
if Pass == "hi":
print "Granted access."
elif Pass == "swag":
print "really?"
else:
print "Wrong, try again."
print "Wrong", tries, "try."
tries = tries + 1
print "Password?"
Pass = raw_input()
if Pass == "hi":
print "Granted access."
else:
print "Wrong,", tries, "tries.try again?"
tries = tries + 1
answer = raw_input()
if answer == "Yes":
print "Password?"
Pass = raw_input()
if Pass == "hi":
print "WOW SUCH PASS"
else:
print "Wrong,", tries, "tries...aborting"
tries = tries + 1
else:
print "Okay."
答案 3 :(得分:0)
自由改写:
users = {
'hi': 'hi',
'swag': None,
'nsa': 'hail_the_fatherland'
}
def validate_user():
for tries in range(3):
user = raw_input('User: ')
pwd = raw_input('Password: ')
if user in users and users[user] == pwd:
print('Access Granted')
return True
print('Go away, you l33t h4ck3r')
return False
def main():
if validate_user():
print('Direct me, master!')
if __name__=="__main__":
main()