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"):
...
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.
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.")
Starting at "#If user does not have account:" the code is printing the output multiple times. I just want it to print the output (either the username is taken or they can continue) once. Then, I want the password to be typed twice and compared to make sure that they match. Can you help me fix this issue?
import colorama
colorama.init()
print_in_green = "\x1b[32m"
print_in_red = "\x1b[31m"
print_in_blue = "\x1b[36m"
print_in_pink = "\x1b[35m"
print_default = "\x1b[0m"
#STAGE 1: Opening the files and grabbing data
users_path = "c:\\Users\\Anna Hamelin\\Documents\\Python Scripts\\SourceCode\\Project2\\usernames.txt"
passwords_path = "c:\\Users\\Anna Hamelin\\Documents\\Python Scripts\\SourceCode\\Project2\\passwords.txt"
scoreslist_path = "c:\\Users\\Anna Hamelin\\Documents\\Python Scripts\\SourceCode\\Project2\\scores.txt"
def get_file_contents(file_path):
return [line.strip() for line in open(file_path)]
scoreslist = get_file_contents(scoreslist_path)
def add_file_contents(file_path, contents):
with open(file_path, "a") as file:
file.write(contents)
def login_user(new_account=False):
usernameslist = get_file_contents(users_path)
passwordslist = get_file_contents(passwords_path)
if new_account:
response = 'y'
else:
response = input("-"*50 + "\nWelcome! Do you have an account (y/n)? ")
print("-"*50)
#If user has an account:
if response == "y":
goodlogin = False
username = input("Please enter your username: ")
password = input("Please enter your password: ")
for id in range(len(usernameslist)):
if username == usernameslist[id] and password == passwordslist[id]:
goodlogin = True
if goodlogin:
print(print_in_green + "Access granted!" + print_default)
#Ask if user would like to view leaderboard
leaderboard = input("Would you like to view the leaderboard (y/n)? ")
#If thet want to see scores:
if leaderboard == "y":
print("-"*50 + "\n" + print_in_blue + "Here is the leaderboard!\n" + print_default + "-"*50)
for c in range(0, len(scoreslist)-1):
max = scoreslist[c]
index_of_max = c
for i in range (c+1, len(scoreslist)):
if (scoreslist[i] > max):
max = scoreslist[i]
index_of_max = i
aux = scoreslist[c]
scoreslist[c] = max
scoreslist[index_of_max] = aux
#print(scoreslist)
print(*scoreslist, sep = "\n")
print("-"*50)
#If they don't want to see scores:
else:
print("OK. Thanks for loging in!")
else:
print(print_in_red + "Incorrect Login credentials, please try again by restarting." + print_default)
#If user does not have account:
else:
goodlogin2 = False
newusername = input("What is your new username? ")
for id in range(len(usernameslist)):
if newusername != usernameslist[id]:
goodlogin2 = True
print("Ok, please continue!")
else:
print("This username is already taken. Please try another.")
newpassword = input("What is your new password? ")
newpasswordagain = input("Please enter your new password again.")
if newpassword == newpasswordagain:
print("Please follow the instructions to log in with your new credentials.")
add_file_contents(users_path, '\n' + newusername)
add_file_contents(passwords_path, '\n' + newpassword)
login_user(new_account=True)
else:
print("Your passwords do not match. Please try again.")
login_user()
The problem with your code lies in the fact that you are printing the message "Ok, please continue!" within your for loop.
...
for id in range(len(usernameslist)):
if newusername != usernameslist[id]:
goodlogin2 = True
print("Ok, please continue!")
else:
print("This username is already taken. Please try another.")
...
What happens is that every time your usernameslist[id] is checked against the 'newusername' variable, the print statement 'print("Ok, please continue!")' is run, causing the message to be displayed multiple times.
What you can do to fix this issue is to move the print statement out of the for loop, so that, assuming that a new username does not match any username in the usernameslist, the message "Ok, please continue!" will be displayed once to the user before they are prompted to input their password twice.
Here's what should work for you:
...
for id in range(len(usernameslist)):
if newusername != usernameslist[id]:
goodlogin2 = True
else:
print("This username is already taken. Please try another.")
if goodlogin2 = True:
print("Ok, please continue!")
newpassword = input("What is your new password? ")
newpasswordagain = input("Please enter your new password again.")
...
It looks like you're iterating over your whole list of usernames and printing out a success or error message with each iteration.
Try this instead:
# If user does not have account:
else:
goodlogin2 = False
newusername = input("What is your new username? ")
if newusername in usernameslist:
print("This username is already taken. Please try another.")
else:
goodlogin2 = True
print("Ok, please continue!")
# for id in range(len(usernameslist)):
# if newusername != usernameslist[id]:
# goodlogin2 = True
# print("Ok, please continue!")
# else:
# print("This username is already taken. Please try another.")
newpassword = input("What is your new password? ")
newpasswordagain = input("Please enter your new password again.")
if newpassword == newpasswordagain:
print("Please follow the instructions to log in with your new credentials.")
add_file_contents(users_path, '\n' + newusername)
add_file_contents(passwords_path, '\n' + newpassword)
login_user(new_account=True)
else:
print("Your passwords do not match. Please try again.")
This produces the following output for a user with no previous account who enters a name which has not been used:
--------------------------------------------------
Welcome! Do you have an account (y/n)? n
--------------------------------------------------
What is your new username? not_taken2
Ok, please continue!
What is your new password? pwd2
Please enter your new password again.pwd2
Please follow the instructions to log in with your new credentials.
Please enter your username:
The code will only let me guess once . Can someone please tell me what is wrong with my code?
Challenge:
Write a program that sets a password as ‘Gain Access ’ and asks the
user to enter the password and keeps asking until the correct password
is entered and then says ‘Accepted’. The program should count how many
attempts the user has taken and tell them after they have been
accepted.
enter code here
password = 'Gain access'
count = 0
input = input("Enter the password: \n")
while input != password:
print("Incorrect password! Please try again: \n")
count = count + 1
print("You have now got your password wrong " + str(count) + " times. \n")
if(count < 5):
print("Access denied, please contact security to reset your password.")
break
else:
print("Accepted, welcome back.")
print("You had " + str(count) + " attempts until you got your password right.")
You should always include the language you're programming in like simonwo mentioned already.
Looks like Python to me though. I suppose this line input = input("Enter the password: \n") needs to go after while input != password:, as well. Otherwise you can only enter the password once and then it directly executes all 5 loops. But you should NOT assign input because this is the function you want to obtain the input from.
Do something like user_input = input("Enter the password: \n"). So your code should look something like this:
...
user_input = input("Enter the password: \n")
while user_input != password:
print("Incorrect password! Please try again: \n")
user_input = input("Enter the password: \n")
... Your existing code here
But note that this way the user won't get notified if they entered the correct password with their first try. You could insert a check after the first reading of the user input and if it matches the desired password print your welcome phrase.
When I run my program, if I give the right answers to the input, it works perfectly and logs in. But when I give wrong answer, it starts again and when it starts again on a loop, this time even if I give the right answer, it keeps telling me "wrong nickname or password." How do I solve this problem?
kadi = open('kullanici.txt','r')
sif = open('sifre.txt','r')
while True:
ad = input('Your nickname: ')
sifreniz = input('Your password: ')
if not(ad in kadi.read()) or not(sifreniz in sif.read()):
print('Wrong nickname or password.')
continue
else:
print('You succesfully logged in.')
break
you can read the file at once in variable , and then check in variable :
kadi = open('kullanici.txt','r').read()
sif = open('sifre.txt','r').read()
while True:
ad = input('Your nickname: ')
sifreniz = input('Your password: ')
if not(ad in kadi) or not(sifreniz in sif):
print('Wrong nickname or password.')
continue
else:
print('You succesfully logged in.')
break