Python Forum

Full Version: Multiple Passwords
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I am able to get it to prompt one password, but not all 3. Here is an example of my code:

import getpass

thislist = ["Sun", "Moon", "Star"]
passwds = 'Sun, Moon, Star'

password = input("Please enter a password: ")
if password == "Sun":
    print("Welcome, Continue on to the Quiz!")

else:
        print("Login failed, Sorry. Please Try Again!")
password = input("Please enter a password: ")
^^^ This prompts the password, but when I add the moon/star near "if password ==" it does not work. We are working on adding lists to prompt passwords.
(Sep-21-2019, 06:38 PM)NotPythonQueen Wrote: [ -> ]^^^ This prompts the password, but when I add the moon/star near "if password ==" it does not work.
show the exact code that fails...
(Sep-21-2019, 06:41 PM)buran Wrote: [ -> ]
(Sep-21-2019, 06:38 PM)NotPythonQueen Wrote: [ -> ]^^^ This prompts the password, but when I add the moon/star near "if password ==" it does not work.
show the exact code that fails...

import getpass
 
thislist = ["Sun", "Moon", "Star"]
passwds = 'Sun, Moon, Star'
 
password = input("Please enter a password: ")
if password == "Sun" "Moon" "Star" :
    print("Welcome, Continue on to the Quiz!")
 
else:
        print("Login failed, Sorry. Please Try Again!")
password = input("Please enter a password: ")
if password == "Sun" "Moon" "Star" : is same as if password == "Sun": if "Sun" == "SunMoonStar":
you can do
if password in ("Sun", "Moon", "Star"): # you can use list instead of tuple...
this will also work but the former is better
if password == "Sun" or password == "Moon" or password == "Star":
Anyway to have it use my list above? That works but also just wondering.
of course you can do

if password in thislist:
One minor nitpick: if "Sun" == "Sun" "Moon" "Star": is actually equivalent to if "Sun" == "SunMoonStar":. Consecutive strings concatenate.
Why doesn't it work when I try to use a dictionary instead of list?

thisdict = {
  "1": "sun",
  "2": "moon",
  "3": "star"
} 
with the same code?
Because x in thisdict checks against the keys of the dictionary (1, 2, and 3). You would have to check x in thisdict.values(). Note that checking against the keys is very fast in Python, but checking against the values is probably a bit slower than using a list.
(Sep-22-2019, 01:19 AM)ichabod801 Wrote: [ -> ]Because x in thisdict checks against the keys of the dictionary (1, 2, and 3). You would have to check x in thisdict.values(). Note that checking against the keys is very fast in Python, but checking against the values is probably a bit slower than using a list.

okay i will use it like this, but it still didn't work

thisdict = {
  "sport": "football",
  "drink": "sprite",
  "food": "pizza"
} 
password = input("Please enter a password: ")
if password x in thisdict.values()
    print("Welcome!")
Pages: 1 2