2

Just started learning python a couple days ago and have been trying to use what code I know to practice a basic code of asking for a user name and password from a list. I know there are far better/cleaner/matching user to password inputs but I'm just playing with what I know at this point.

users = ['Jon','Joe', 'Jole']

user_input = input('Username: ')

while user_input != users:
    user_redo = input("I'm sorry but we dont recognize you. Please try another username: ")

this is where my problem is. Is there a simple way of breaking the loop if the user enters a matching username from the list?

passwords = ['donkey808','Hanna5006']
password = input('Password: ')

I guess the same question would apply to the password entry as well

while password != passwords:
    pw_redo = input(f'Please enter correct password for user {user_input}: ')
else:
    print(f'Access Granted {user_input}')
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • change `while user_input != users:` to `while user_input not in users:` – eyllanesc Jul 04 '18 at 23:04
  • 1
    Possible duplicate of [Check if something is not in a list in Python](https://stackoverflow.com/questions/10406130/check-if-something-is-not-in-a-list-in-python) – eyllanesc Jul 04 '18 at 23:05

2 Answers2

0

Just do while user_input not in users:

not in checks if user_input is literally not in users

It might also be better to do if user_input not in users:, I don't see the point of a while.

Sheshank S.
  • 3,053
  • 3
  • 19
  • 39
  • If your answer basically repeats what has been mentioned in the comments (and not even by yourself), it should probably be submitted as Community Wiki entry. – Ondrej K. Jul 04 '18 at 23:37
  • @OndrejK. I was typing my answer here while they were talking. The answer is kind of obvious in this case, i was typing out my answer, I didn't see their answer – Sheshank S. Jul 04 '18 at 23:38
  • Doesn't really look like 7 minutes worth of typing? See, how this could be just pointed out as a suggestion though? – Ondrej K. Jul 04 '18 at 23:40
  • Thank you Sheshank. I was using 'while' because the source I'm using to self study python hasnt gotten to the 'not in' function. I was just poking around and jumping ahead. But the 'not in' makes sense. Thank you. – Jonah Molina Jul 04 '18 at 23:41
  • @JonahMolina if it helped remember to upvote and mark as correct answer :) – Sheshank S. Jul 04 '18 at 23:45
  • @SheshankS. Ok so I tried using the 'if user_input not in' and it works for the first attempt, but with a second attempt, itll proceed to the password step if the user_input was correct or not. That is why I was using the 'while' as a check to loop until the user entered a matching username from the list – Jonah Molina Jul 04 '18 at 23:57
  • @JonahMolina can you make a repl.it for this? I canhelp you better there – Sheshank S. Jul 05 '18 at 00:34
  • It would actually be better to make a new question – Sheshank S. Jul 05 '18 at 00:44
  • @SheshankS. Sorry about that. I wasnt getting a message if it registered my click or not so I clicked it a bunch of times hoping that it would go through / land on accept – Jonah Molina Jul 05 '18 at 00:45
0

Write it like this.

users = ["Jon","Joe", "Jole"]
while 0 < 1 :
  user_input = input('Username: ')
  if user_input not in users:
      print("I'm sorry but we dont recognize you. Please try another username: ")
  elif user_input in users:
    break
Ali Hassan
  • 46
  • 9