My project keeps logging me out, Why is this happening? (CLOSED) - python

I'm writing a code that will allow me to save my data into the database. But for some reason, whenever I type in a response, it refers to the else statement and logs me out.
Here's my code:
username = "storagei"
password = "something"
# asking for a username and password.
userInput = input("welcome back to safespace! Please type in your username to access your data.\n")
if userInput == username:
userInput = input("Password?\n")
if userInput == password:
print("Welcome back to safespace Izzy!")
else:
print("That is the wrong password. Please try again.")
exit(1)
else:
print("There is no username that matches what your responce. Please try again.")
exit(2)
print "You can open_storage, add_to_storage, or log_out"
userInput = input("What would you like to do?\n")
log_out = "Thanks for using safespace. Come back soon!"
open_storage = "opening files."
add_to_storage = "what do you like to add?"
if userInput == log_out:
print("Thanks for using safespace. Come back soon!")
exit(3)
if userInput == open_storage:
print("opening storage. Please wait...")
if userInput == add_to_storage:
print("what would you like to add?")
else:
print("not a valid input. Logging you out.")
So for some reason, when it gets to this part of the code:
print "You can open_storage, add_to_storage, or log_out"
userInput = input("What would you like to do?\n")
log_out = "Thanks for using safespace. Come back soon!"
open_storage = "opening files."
add_to_storage = "what do you like to add?"
if userInput == log_out:
print("Thanks for using safespace. Come back soon!")
exit(3)
if userInput == open_storage:
print("opening storage. Please wait...")
if userInput == add_to_storage:
print("what would you like to add?")
else:
print("not a valid input. Logging you out.")
I have fixed my code on my own. Here is the working script I have with all the edits if you would like to see them.
username = "storagei"
password = "something"
# asking for a username and password.
userInput = input("welcome back to safespace! Please type in your username to access your data.\n")
if userInput == username:
userInput = input("Password?\n")
if userInput == password:
print("Welcome back to safespace Izzy!")
else:
print("That is the wrong password. Please try again.")
exit(1)
else:
print("There is no username that matches what your responce. Please try again.")
exit(2)
print "You can open_storage type in o. add_to_storage type in a. log_out type in l. delete_files type in d. or edit_files type in e."
userInput = input("What would you like to do?\n")
l = "l"
o = "o"
a = "a"
d = "d"
e = "e"
if userInput == l:
print("Thanks for using safespace. Come back soon!")
exit(3)
if userInput == o:
print("opening storage. Please wait...")
if userInput == a:
print("what would you like to add?")
if userInput == d:
print("what file do you want to delete?")
if userInput == e:
print("what file do you want to edit?")
else:
if userInput != l:
if userInput != o:
if userInput != a:
if userInput != d:
if userInput != e:
print("not a valid input. Logging you out.")

Try this
if userInput == 'open_storage':
print("opening storage. Please wait...")
elif userInput == 'add_to_storage':
print("what would you like to add?")
elif userInput == 'log_out':
print("Thanks for using safespace. Come back soon!")
exit(3)
else:
print("not a valid input. Logging you out.")

Related

Validate User Input I don't want it to start over when I say no after putting in the wrong input

I am trying to validate a users input by using while True:
When I type the wrong input it tells the user invalid, but when I say no I don't want to continue the code still start's over when I want it to exit the program. How would I fix this?
Output text to the console
def play_again():
print("")
incorrect = ""
while incorrect != "yes" and incorrect != "no":
incorrect = input("Not a valid entry.\n"
"Please choose yes or no:\n").lower()
if "y" in incorrect:
break
elif "n" in incorrect:
break
return incorrect
while True:
print("")
intro()
# Output text to the console
print("")
make_choice()
accept = input("would you like to play again? \n"
"Type yes or no: ").lower()
if 'n' in accept:
print_pause("Thanks for playing!")
break
elif 'y' in accept:
print_pause("Awsome! Lets play again.")
else:
play_again()
All of the logic should be in the play_again function. It can return True if the user wants to play again, else False.
def play_again():
accept = input("would you like to play again?\nType yes or no: ").lower()
while True:
if accept in ["y", "yes"]:
print_pause("Awesome! Lets play again.")
return True
elif accept in ["n", "no"]:
print_pause("Thanks for playing!")
return False
accept = input("Not a valid entry.\nPlease choose yes or no:\n").lower()
while True:
print("")
intro()
print("")
make_choice()
if not play_again():
break

Why won't my print statements show up in my elif block?

I know there is probably a simple solution to this, but the text I wrote under the elif sections in userName and passWord will not print when a user successfully logs in. Please help!
def userName():
username = "kate"
userInput = input('Please enter your username:\n')
while userInput != username:
if len(userInput) == 0:
print("Your username can not be blank, please try again!\n")
userInput = input('Please enter your username.\n')
elif userInput == username:
print("Welcome back, " + username)
print("")
break
else:
print("Sorry, that username is not in our system. Please try again!\n")
userInput = input('Please enter your username.\n')
def passWord():
password = "stags"
passInput = input("Please enter your password:\n")
while passInput != password:
if len(passInput) == 0:
print("Your password can not be blank, please try again!\n")
passInput = input("Please enter your password.\n")
elif passInput == password:
print("You have successfully logged in!\n")
break
else:
print("Sorry, your password is invalid. Please try again!")
passInput = input("Please enter your password.\n")
def main():
print("Hello, let's get started!")
print("")
userName()
passWord()
main()
This was pointed out in the comments above, but if you change
while userInput != username:
and
while passInput != password:
to
while True:
it should work just fine. Then it forces your code to hit the elif statement rather than breaking the loop before printing what you want to say.
In python indentation indicates where a code block starts and begins. So in your while loop in passWord() your if..elif..else must be indented exactly one tab in eg.
while passInput != password:
if len(passInput) == 0:
print("Your password can not be blank, please try again!\n")
passInput = input("Please enter your password.\n")
elif passInput == password:
print("You have successfully logged in!\n")
break
else:
print("Sorry, your password is invalid. Please try again!")
passInput = input("Please enter your password.\n")
Notice how the indentation always goes one tab in for each code block you want to create.

How to get "Correct enter the maze" without getting "sorry, please try again"

password = "nothing"
tries = 0
while password != "secret":
tries = tries + 1
password = input("What is the secret password? ")
print("sorry, please try again. ")
if tries == 3:
print("You have been locked out")
exit()
print("Correct! Enter the maze!")
when i write the correct password i get this in return
What is the secret password? secret.
sorry, please try again.
Correct! Enter the maze
Python has no do-while, so one of ways to do similar thing is
password = ""
tries = 0
while True:
password = input("What is the secret password? ")
if password == "secret":
break
print("sorry, please try again. ")
tries += 1
if tries == 3:
print("You have been locked out")
exit()
print("Correct! Enter the maze!")
You can use this code:
password = "nothing"
tries = 0
while password != "secret":
tries = tries + 1
password = input("What is the secret password? ")
if password == "secret":
break
else:
print("sorry, please try again. ")
if tries == 3:
print("You have been locked out")
exit()
print("Correct! Enter the maze!")
If you do not want the password to be seen, you can use the following library
getpass()
example:
import getpass
p = getpass.getpass(prompt='What is the secret password? ')
if p.lower() == 'secret':
print('Correct! Enter the maze!')
else:
print('sorry, please try again.')

Simple looping if password

while True:
look = input("Key : ")
if look == "ksvbd956dmew1":
But instead of that and having a bunch of elifs can I do like
while True:
look = input("Key : ")
if look == "ksvbd956dmew1", "Otherpassword", "Otherpassword":
Then if the user types one of those passwords it lets them in. I wasn't sure what this was called as I am new to python so I couldn't search it up Hopefully someone could help me here :)
Try this,
passwordList = ("ksvbd956dmew1", "Otherpassword", "Otherpassword")
password = input("Please enter password : ")
while True:
try:
if password in passwordList:
print("Password is Correct")
break
else:
raise Exception
except:
print("Password is wrong")
password = input("Please enter password : ")
while True:
look = input("Key : ")
if look in ("ksvbd956dmew1", "Otherpassword", "Otherpassword"):
...

How do I create this loop? [duplicate]

This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 3 years ago.
How do I create this loop where if welcome is not equal to "yes" or "no", it repeats the question (if they have an account), but if welcome is equal to "yes" or "no", they create an account (for "no") or allow the user to login (for "yes")?
Thanks in advance
Below is my code used :
welcome = input("Do you have an account? Please type either 'yes'/'no': ")
if welcome == "no":
while True:
username = input("OK. Let's create an account. Enter a username:")
password = input("Enter a password:")
password1 = input("Confirm password:")
if password == password1:
file = open(username+".txt", "w")
file.write(username+":"+password)
file.close()
welcome = "yes"
break
print("Passwords do NOT match!")
if welcome == "yes":
while True:
login1 = input("OK. Enter your username:")
login2 = input("Enter your password:")
file = open(login1+".txt", "r")
data = file.readline()
file.close()
if data == login1+":"+login2:
print("You have successfully logged in as, " + login1)
print("")
print("Welcome to the Music Quiz!")
break
print("Incorrect username or password.")
Put a loop around asking for welcome.
while True:
welcome = input("Do you have an account? Please type either 'yes'/'no': ")
if welcome in ("yes", "no"):
break
if welcome == "no":
...
else:
...
You can add a bool variable that keeps track of user's input status (valid/invalid)
validAnswer = False:
while not validAnswer:
welcome = input("Do you have an account? Please type either 'yes'/'no': ")
if welcome == "no":
validAnswer = True
while True:
username = input("OK. Let's create an account. Enter a username:")
password = input("Enter a password:")
password1 = input("Confirm password:")
if password == password1:
file = open(username+".txt", "w")
file.write(username+":"+password)
file.close()
welcome = "yes"
break
print("Passwords do NOT match!")
elif welcome == "yes":
validAnswer = True
while True:
login1 = input("OK. Enter your username:")
login2 = input("Enter your password:")
file = open(login1+".txt", "r")
data = file.readline()
file.close()
if data == login1+":"+login2:
print("You have successfully logged in as, " + login1)
print("")
print("Welcome to the Music Quiz!")
break
print("Incorrect username or password.")
You can simply add a while True: to the whole thing, this way, when a "wrong" input is entered, it will fall through both of the if clauses and return to the beginning of the loop, where it will prompt the user again.
while True:
welcome = input("Do you have an account? Please type either 'yes'/'no': ")
if welcome == "no":
while True:
username = input("OK. Let's create an account. Enter a username:")
password = input("Enter a password:")
password1 = input("Confirm password:")
if password == password1:
file = open(username+".txt", "w")
file.write(username+":"+password)
file.close()
welcome = "yes"
break
print("Passwords do NOT match!")
if welcome == "yes":
while True:
login1 = input("OK. Enter your username:")
login2 = input("Enter your password:")
file = open(login1+".txt", "r")
data = file.readline()
file.close()
if data == login1+":"+login2:
print("You have successfully logged in as, " + login1)
print("")
print("Welcome to the Music Quiz!")
break
print("Incorrect username or password.")

Categories

Resources