0

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.

asked Jun 5, 2017 at 19:55
4
  • What is the desired behaviour? Commented Jun 5, 2017 at 20:01
  • Hypothetically, what happens if you do while count != len(usernames): instead? Commented Jun 5, 2017 at 20:01
  • Seems like there may be some hidden newlines in the user input from raw_input username and "m.leeman\n", At the line after you have indicated "#problem somewhere here", try if username.strip()==usernames[count].strip(): to clean the strings and strip-off the newlines. Worked when I tried it. Commented Jun 5, 2017 at 20:07
  • A word of advice - use a dictionary with usernames as keys and passwords as values. It will be much easier to handle and won't require all this loops. Commented Jun 5, 2017 at 20:12

2 Answers 2

1

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

answered Jun 5, 2017 at 20:26
Sign up to request clarification or add additional context in comments.

Comments

0

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.

answered Jun 5, 2017 at 20:02

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.