This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 4 years ago.
I would like the program to ask the user for the password if the password wasnt correct. How do I do it?
#program that saves user passwords
user_input = ""
FB = input("what is your facebook pasword \n")
print("your facebook passoerd is " + FB + " is this correct?")
user_input = input()
if (user_input == "yes"):
print("password has been saved")
elif user_input == "no":
print("password was not saved")
else:
print("i do not understand. sorry")
Welcome to StackOverflow!
One of the usual tricks is to wrap all of that in a while loop and break the loop if user input is anything other than "no"
while True:
user_input = ""
FB = input("what is your facebook pasword \n")
print("your facebook password is " + FB + " is this correct?")
user_input = input()
if user_input is "yes":
print("password has been saved")
break #add break here to exit the program
elif user_input is "no":
print("password was not saved")
else:
print("i do not understand. sorry")
break #add break here to exit the program
Related
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.
This question already has an answer here:
Searching array reports "not found" even though it's found
(1 answer)
Closed 1 year ago.
My code is always saying it is incrorrect even tough it is present on the text file,
it was working before but now isn't for some reason.
def select_login_signup():
while True:
selection = input("Welcome to sports.com, please select"
" \"L\" to Log In with your account or \"S\" to create an account: ")
if selection.lower() == 's':
register()
answer = input("Would you like to Log In? Y/N? ")
while not answer:
if answer.lower() == "y":
login()
break
elif answer.lower() == "n":
exit()
else:
answer = False
print("Invalid answer.")
continue
elif selection.lower() == 'l':
login()
break
else:
print("Invalid answer.")
continue
def register():
username = input("Create your username (no more than 10 characters or less than 4.): ")
while 10 < len(username) < 4:
print('username cannot have more than 10 characters or less than 4.')
username = input("Create your username (no more than 10 characters or less than 4.):
")
break
while username.isnumeric():
print("username must contain at least one letter")
username = input("Create your username (no more than 10 characters or less than 4.):
")
break
password = input("Create a password with letters and numbers: ")
while len(password) < 6:
print("Your password must contain more than 6 characters.")
password = input("Create a password with letters and numbers: ")
continue
while password.isnumeric() or password.isalpha():
print("Your password must contain both letters and numbers")
password = input("Create a password with letters and numbers: ")
continue
login_credentials = open('C:\\Users\\hmarq\\Documents\\UsernameAndPassword.txt', "a")
login_credentials.write(f'\n{username},{password}')
login_credentials.close()
print("Account created successfully.")
def login() -> object:
username = input("Please enter your username: ")
username = username.strip()
password = input("Please enter your password: ")
password = password.strip()
login_credentials = open('C:\\Users\\hmarq\\Documents\\UsernameAndPassword.txt', "r")
login_credentials.readlines()
with open('C:\\Users\\hmarq\\Documents\\UsernameAndPassword.txt', 'r') as
login_credentials:
for line in login_credentials:
login_info = line.split(",")
if username == login_info[0] and password == login_info[1]:
print("Authorized")
authenticated = True
return authenticated
else:
print("Incorrect credentials.")
username = input("Please enter your username: ")
username = username.strip()
password = input("Please enter your password: ")
password = password.strip()
continue
def main():
select_login_signup()
if __name__ == "__main__":
main()
with open('C:\Users\hmarq\Documents\UsernameAndPassword.txt', 'r') as login_credentials
when you open a file, you have to format the data and therefore the data is not the same.
if username == login_info[0] and password == login_info[1]:
I hope it serves you, greetings.
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.')
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.")
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.")