|
|
3鱼币
大佬们好,
新手写了一个很麻烦的代码,但是还是不死心想针对遇到的问题咨询一下,贡献全部鱼bi
a.问题是python第九课的作业题,如图:
0. 设计一个验证用户密码程序,用户只有三次机会输入错误,不过如果用户输入的内容中包含"*"则不计算在内。

b.代码如下:
temp = input("give the password, please:")
guess = str(temp)
password = "xixixi"
i=1
flag = 0
while i<4:
if guess == password:
flag = 1
break
elif i == 1:
result = "*" in input()
if result == False:
print("you have still 2 chances")
i=i+1
else:
print("no * please, you have still 3 chances")
elif i == 2:
result = "*" in input()
if result == False :
print("you have still 1 chances")
i=i+1
else:
print("no * please, you have still 2 chances")
elif i == 3:
result = "*" in input()
if result == False:
print("you have no more chance")
else:
print("no * please, you have still 1 chances")
if flag == 1:
print("right, loading")
c. 能完成要求功能,但第一次输入的密码会被自动无视,从第二次输入密码才开始完成功能要求,如下,当第一次输入123456时,系统没有任何反应。
give the password, please:123456
123456
you have still 2 chances
123456*
no * please, you have still 2 chances
123456*
no * please, you have still 2 chances
123456
you have still 1 chances
123456
you have no more chance
d. 刚开始学习python,自己都知道自己写的很冗杂,有无数好的实现方法,但只是想知道自己错在哪里,以后可以避免。
最后不喜勿喷,如果觉得很鄙视请直接绕行,最后谢谢大家的支持~~
password = "xixixi"
i = 1
flag = 0
while i < 4:
temp = input("give the password, please:")
guess = str(temp)
if guess == password:
flag = 1
break
else:
if "*" not in temp:
print("Password error,You have still %d chances" % (3 - i))
i += 1
else:
print("no * please,,You have still %d chances" % (3 - i))
if flag == 1:
print("right, loading")
|
最佳答案
查看完整内容
password = "xixixi"
i = 1
flag = 0
while i < 4:
temp = input("give the password, please:")
guess = str(temp)
if guess == password:
flag = 1
break
else:
if "*" not in temp:
print("Password error,You have still %d chances" % (3 - i))
i += 1
else:
print("no * please,,You have still %d chances" % (3 - ...
|