2017-06-05 61 views
-1

我在菜单系统的某些代码中遇到了一些问题。到目前为止,我没有完成但我遇到了问题。它可以和account.leeman一起工​​作,但我永远不会得到'管理员'的工作,它总是回滚到开始。我已经看了一段时间的代码,但我无法弄清楚为什么这是为什么它永远不会继续。 “添加”的部分代码可以忽略不计,因为它不是一个问题还没有,但如果你能改善它看到任何方式,请让我知道Python 3中的循环

所以这是我:

usernames=["m.leeman","administrator"] 
passwords=["pA55w0rd","password"] 
while True: 
    count=0 
    add_or_enter=input("Would you like to Enter a username or Add a new one?: ").lower() 
    if add_or_enter=="enter": 
     username=input("Please enter username: ") 
     while (count+1)!=(len(usernames)): 
      #problem somewhere here 
      if username==usernames[count]: 
       print("Username accepted") 
       password=input("Please enter password: ") 
       if password==passwords[count]: 
        print("Welcome "+str(username)) 
        print("Continue here") 
       else: 
        print("Incorrect password") 
      else: 
       count+=1 
       if count==len(usernames): 
        print("User does not exit") 
       else: 
        () 
        #should run again after this 
    elif add_or_enter=="add": 
     new_user=input("Enter a new username: ") 
     if (new_user=="add") or (new_user==usernames[count]): 
      print("Username unavailiable") 
     else: 
      count=+1 
      if count==len(usernames)-1: 
       usernames.append(new_user) 
       new_password=input("Enter a password for this user: ") 
       passwords.append(new_password) 
       print("User "+new_user+" successfully created") 

任何回复将非常有帮助。谢谢。

+0

什么是理想的行为? – roganjosh

+0

假设,如果你做'while count!= len(username):'会发生什么? – Kevin

+0

似乎在用户输入中可能存在一些来自'raw_input''用户名'和''m.leeman \ n“'的隐藏换行符,在你指出''#problem某处在''之后'的行后,尝试'if username.strip()==用户名[count] .strip():'清理字符串并剥离换行符。工作时,我试了一下。 – davedwards

回答

0

你永远不会得到管理员,因为你是双倍递增计数。你有计数增加的条件,然后在else语句中,当它不= m.leeman所以在else语句后取出计数+ = 1。

1

让我们看看在这行代码:

while (count+1)!=(len(usernames)): 

在第一循环中,计数为0,因此0 + 1 = 2和循环执行!。他们输入的名字“administrator”不匹配“m.leeman”,所以count增加1,循环将再次执行。

这一次,count = 1。所以count + 1 = 2,这恰好是用户名的长度。该循环不执行并且未找到管理员帐户。

解决方案?拆下+ 1

while count != len(usernames): 

希望这有助于