Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiple Passwords
#1
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.
Reply
#2
(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...
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(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: ")
Reply
#4
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":
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Anyway to have it use my list above? That works but also just wondering.
Reply
#6
of course you can do

if password in thislist:
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
One minor nitpick: if "Sun" == "Sun" "Moon" "Star": is actually equivalent to if "Sun" == "SunMoonStar":. Consecutive strings concatenate.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
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?
Reply
#9
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#10
(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!")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Hashing Passwords (HELP) MianDoesCoding 4 2,179 May-26-2020, 03:11 PM
Last Post: buran
  Writing incorrect passwords to a file till its correct garth 2 4,954 Feb-10-2017, 11:41 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020