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.
Related
It can see the file, if i change the name on the path slightly run seizes to function but eithr way it can't read the words in the file despite being able to before while I was writing the program. And now suddenly it doesn't work, the file location is the same and all.
P.S. I have no idea how to specify code on overflow
filen = 'C:\Users\fabby\Documents\Extra Things I Might Need\Port Folio Stuff\Python\usernames'
usern = open(filen, 'r')
userr = input("Enter your Username: ")
ass = input("Enter your Password: ")
def func():
user = input("Enter new Username: ")
passs = input("Enter new Password: ")
passs1 = input("Confirm password: ")
if passs != passs1:
print("Passwords do not match!")
else:
if len(passs) <= 6:
print("Your password is too short, restart:")
elif user in usern:
print("This username already exists")
else:
usern = open(filen, "a")
usern.write(user+", "+passs+"\n")
print("Success!")
while True:
if userr not in usern:
again = input("This username does not exist, would you like to try again? ")
if again == ("No"):
func()
elif again == ("no"):
func()
elif again == ("Yes"):
print("Try again:")
userr = input("Enter your Username: ")
ass = input("Enter your Password: ")
elif again == ("yes"):
print("Try again:")
userr = input("Enter your Username: ")
ass = input("Enter your Password: ")
elif userr in usern:
print("Good, you have entered the zone")
I am not sure I have full understand your means, but as your code, I have some suggestions:
close file if you open it
use f.readline() and str.split() to parse username and passwd and store in array, so you can use in to check, if the file is not to large.
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.")
I've been searching around nearly all morning looking for a piece of code that can help me here but its hard to find one that is similar!
I have to create a bank system that asks the user to input a username and password.
If these are entered 3 times the system shuts down.
So far, i have got my program to know if the password/username is correct or not.
Now i just need to figure out how to make it run and stop after 3 incorrect attempts.
Really appreciate any help given on this one! Thanks
Code:
username = "bank_admin"
password = "Hytu76E"
usernameGuess = raw_input("Please enter your username: ")
passwordGuess = raw_input("Please enter the password: ")
while (username != usernameGuess or password != passwordGuess):
print ("Please try again.")
usernameGuess = raw_input("Please enter your username: ")
passwordGuess = raw_input("Please enter your password: ")
print ("Password accepted. Access Authorized.")
You can add a counter to see how many times they guessed the wrong password. Then use that as another condition in your while loop.
incorrectGuesses = 0
correct = False
while (not correct and incorrectGuesses < 4):
usernameGuess = raw_input("Please enter your username: ")
passwordGuess = raw_input("Please enter your password: ")
correct = ((username == usernameGuess) and (password == passwordGuess))
if not correct:
print ("Please try again.")
incorrectGuesses += 1