Posts: 7
Threads: 1
Joined: Sep 2019
Sep-21-2019, 06:38 PM
(This post was last modified: Sep-21-2019, 06:40 PM by buran.)
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.
Posts: 8,160
Threads: 160
Joined: Sep 2016
(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...
Posts: 7
Threads: 1
Joined: Sep 2019
Sep-21-2019, 06:47 PM
(This post was last modified: Sep-21-2019, 06:47 PM by NotPythonQueen.)
(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: ")
Posts: 8,160
Threads: 160
Joined: Sep 2016
Sep-21-2019, 06:51 PM
(This post was last modified: Sep-22-2019, 06:19 AM by buran.)
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":
Posts: 7
Threads: 1
Joined: Sep 2019
Anyway to have it use my list above? That works but also just wondering.
Posts: 8,160
Threads: 160
Joined: Sep 2016
of course you can do
if password in thislist:
Posts: 4,220
Threads: 97
Joined: Sep 2016
One minor nitpick: if "Sun" == "Sun" "Moon" "Star": is actually equivalent to if "Sun" == "SunMoonStar": . Consecutive strings concatenate.
Posts: 7
Threads: 1
Joined: Sep 2019
Sep-22-2019, 12:44 AM
(This post was last modified: Sep-22-2019, 12:45 AM by NotPythonQueen.)
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?
Posts: 4,220
Threads: 97
Joined: Sep 2016
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.
Posts: 7
Threads: 1
Joined: Sep 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!")
|