I'm having some problems with some code for a menu system. What I have so far isn't completed but I'm having a problem with it. It works fine with the account.leeman' but I can never get 'administrator' to work, and it always rolls back to the beginning. I've looked at the code for a while now but I can't figure out why this is and why it doesn't ever continue. The 'add' part of the code can be ignored as it isn't a problem yet, although if you can see any way in improving it, please let me know
So this is what I have:
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")
Any replies will be very helpful. Thanks.
2 Answers 2
Lets take a look at this line of code:
while (count+1)!=(len(usernames)):
On the first loop, count is 0 so 0+1 != 2 and the loop executes. Their inputted name "administrator" won't match "m.leeman" so count is increased by one and the loop will execute again.
This time, count = 1. So count + 1 = 2, which happens to be the length of usernames. The loop doesn't execute and the admin account is not found.
The solution? Remove the + 1
while count != len(usernames):
Hope this helps
Comments
you never get to administrator because you are double incrementing count. you have count being incremented in the condition and then in the else statement when it doesn't = m.leeman so take out the count+=1 after the else statement.
while count != len(usernames):instead?raw_inputusernameand"m.leeman\n", At the line after you have indicated"#problem somewhere here", tryif username.strip()==usernames[count].strip():to clean the strings and strip-off the newlines. Worked when I tried it.