Python Forum
Multiple Passwords - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Multiple Passwords (/thread-21263.html)

Pages: 1 2


Multiple Passwords - NotPythonQueen - Sep-21-2019

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.


RE: Multiple Passwords - buran - Sep-21-2019

(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...


RE: Multiple Passwords - NotPythonQueen - Sep-21-2019

(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: ")



RE: Multiple Passwords - buran - Sep-21-2019

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":



RE: Multiple Passwords - NotPythonQueen - Sep-21-2019

Anyway to have it use my list above? That works but also just wondering.


RE: Multiple Passwords - buran - Sep-21-2019

of course you can do

if password in thislist:



RE: Multiple Passwords - ichabod801 - Sep-21-2019

One minor nitpick: if "Sun" == "Sun" "Moon" "Star": is actually equivalent to if "Sun" == "SunMoonStar":. Consecutive strings concatenate.


RE: Multiple Passwords - NotPythonQueen - Sep-22-2019

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?


RE: Multiple Passwords - ichabod801 - Sep-22-2019

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.


RE: Multiple Passwords - NotPythonQueen - Sep-22-2019

(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!")