2017-06-05 25 views
-1

私はメニューシステムのいくつかのコードにいくつかの問題があります。私がこれまでにしてきたことは完了していないが、問題がある。それはaccount.leeman 'とうまく動作しますが、私は決して「管理者」に仕事をさせることはできず、常に最初にロールバックします。私はしばらくの間、コードを見てきましたが、なぜこれが、そしてなぜこれが続くのではないのか理解できません。Python 3のwhileループwhile

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(usernames):'を実行するとどうなりますか? – Kevin

+0

'raw_input'' username'と '" m.leeman \ n "'からのユーザ入力に隠れた改行があるかもしれないようですが、 ''どこかのどこかに ''どこかの問題 ' username.strip()==ユーザ名[count] .strip(): 'は文字列を消去し、改行を削除します。私はそれを試したときに働いた。 – davedwards

答えて

0

あなたは二重増分カウントであるため、管理者になることはありません。条件内でカウントがインクリメントされていて、else文でなければelse文の後にcount + = 1を取り出します。

1

は、このコード行を見てみましょう!

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

を最初のループでは、カウントが0ので、0 + 1 = 2を、ループが実行されます。入力された名前 "administrator"は "m.leman"と一致しないので、カウントが1つ増えてループが再び実行されます。

この場合、count = 1です。したがって、count + 1 = 2となります。これはユーザー名の長さになります。ループは実行されず、管理者アカウントは見つかりません。

解決策はありますか? + 1

while count != len(usernames): 

ホープ、これは望ましい動作とは何である