【发布时间】:2019-12-20 01:27:38
【问题描述】:
我正在编写一个涉及登录子例程的代码,它运行良好,除了当我只需要其中一个时它提供两个输出这一事实。子程序如下:
def Login():
chances = 0
Status = True #Status refers to whether the person is logged in or not
while Status is True:
Supplied_Username = input('What is your username?')
Supplied_Password = input('What is your password?')
with open("Loginfile.txt","r") as Login_Finder:
for x in range(0,100):
for line in Login_Finder:
if (Supplied_Username + ',' + Supplied_Password) == line.strip():
print("You are logged in")
game()
else:
print("Sorry, this username or password does not exist please try again")
chances = chances + 1
if chances == 3:
print("----------------------------------------------------\n Wait 15 Seconds")
time.sleep(15)
Login()
sys.exit()
def game():
print('HI')
就像我上面所说的那样,这很有效。当用户输入正确的详细信息时,他们会同时获得:
“您已登录”输出和“抱歉...这些详细信息不存在”输出
我需要做些什么来确保我得到每个场景的正确输出(错误的细节和正确的细节)?
【问题讨论】:
-
for x in range(0, 100):循环的目的是什么?你永远不会使用x做任何事情。 -
如果循环正常结束而不是以
break停止,则执行循环的else:块。所以当你找到你要找的东西时,你需要跳出循环。 -
Python 中的变量名通常不使用大写字母
-
@MadPhysicist 这个问题专门询问输出,而不是返回值。
-
每次递归调用
Login(),都会将chances设置为0,所以永远不会达到3。
标签: python for-loop if-statement while-loop output