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
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.
Could anyone please assist me with the following:
I have a code that reads a username and password then allows users to access a program. I have the first option to register a new user correct. I'm having a problem with an infinite loop problem with my last two lines of code. Id like to run a string stating that if an unregistered username is entered it returns with a string saying that there is no such registered user. The string just keeps running in the loop and is there anything I could do to change this.
username: admin
password: adm1n
my code is as follows:
users = {}
with open ('user.txt', 'rt')as username:
for line in username:
username, password = line.split(",")
users[username.strip()] = password.strip() # strip removes leading/trailing whitespaces
uinput = input("Please enter your username:\n")
while uinput not in users:
print("Username incorrect.")
uinput = input("Please enter a valid username:/n")
if uinput in users:
print ("Username correct")
with open('user.txt', 'rt') as password:
for line in password:
username, password = line.split(",")
users[password.strip()] = username.strip() # strip removes leading/trailing whitespaces
uinput2 = input("Please enter your password:\n")
while uinput2 not in users:
print("Your username is correct but your password is incorrect.")
uinput2 = input("Please enter a valid password:\n")
if uinput2 in users:
password2 = ("Password correct")
print (password2)
if password2 == ("Password correct"):
menu = (input("\nPlease select one of the following options:\nr - register user\na - add task\nva - view all tasks\nvm - view my tasks\ne - exit\n"))
if menu == "r" or menu == "R":
new_user = (input("Please enter a new user name:\n"))
new_password = (input("Please enter a new password:\n"))
with open ('user.txt', 'a')as username:
username.write("\n" + new_user + ", " + new_password)
elif menu == "a" or menu == "A":
task = input("Please enter the username of the person the task is asigned to.\n")
while task not in username:
print("User not registered. Please enter a valid username:\n")
You have a loop at the end that says
while task not in username:
print("User not registered. Please enter a valid username:\n")
This is unfinished and will loop endlessly since if task is not in username, printing something will not change that fact so it will just loop and print again. You probably wanted to add something like
task = input("Please enter a valid username of a person the task is assigned to.\n")
I need to be able to validate the user and the password inputted, but when I run the code below, I'm able to verify only the first element of the list and the second element and so on aren't being verified.
Note: The user and password are stored in the list as class objects [like this:
admin(user, password)]...
def login(self):
user_name = input("Please Enter Your Username : ").upper()
password = input("Please Enter Your Password : ").upper()
for obj in self.admins:
while obj.admin_name != user_name and obj.admin_password != password:
print(" Sorry Username and Password Incorrect Please Re-enter for Validation ")
user_name = input("Please Enter Your Username : ").upper()
password = input("Please Enter Your Password : ").upper()
else:
print("Greetings,", user_name, "You are Now Logged in the System")
break
When you run break in your else branch you are actually calling it on the for loop. Remove the break and it should be working as you expect it to
Your while loop only checks for the first user name. You should switch the order of your loops:
def login(self):
user_name = input("Please Enter Your Username : ").upper()
password = input("Please Enter Your Password : ").upper()
while True:
for obj in self.admins:
if obj.admin_name == user_name and obj.admin_password == password:
break
else:
print(" Sorry Username and Password Incorrect Please Re-enter for Validation ")
user_name = input("Please Enter Your Username : ").upper()
password = input("Please Enter Your Password : ").upper()
continue
break
print("Greetings,", user_name, "You are Now Logged in the System")
This is also a very bad way to check passwords.
Simply remove break from your else statement.
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.
I am making a password validator/checker program as part of my computing assignment.It must have an uppercase and lowercase letter and be at least 8 characters long.
So far I have done this:
new_password = input("Please enter your new password: ")
new_password2 = input("Please enter your new password again: ")
while new_password != new_password2:
print("The passwords don't match up.")
new_password = input("Please enter your new password: ")
new_password2 = input("Please enter your new password again: ")
if new_password == new_password2:
length = len(new_password)
while int(length) < 8:
print("Your password must be longer")
new_password = input("Please enter your new password: ")
new_password2 = input("Please enter your new password again: ")
letters = set(new_password)
lower = any(letter.islower() for letter in letters)
while new_password == new_password2:
if not lower:
print("Your password must contain a lowercase letter")
new_password = input("Please enter your new password: ")
new_password2 = input("Please enter your new password again: ")
upper = any(letter.isupper() for letter in letters)
while new_password == new_password2 :
if not upper:
print("Your password must contain an uppercase letter")
new_password = input("Please enter your new password: ")
new_password2 = input("Please enter your new password again: ")
The code runs, but for some reason, the while loops do not work as even if the condition is right,( eg. the password contains an uppercase letter), the option for the user to enter the password again is being displayed. Can someone take a look and tell me the problem here? Thanks
You want to only have one while loop in which every requirement is checked. See the following code:
valid_password = False
while not valid_password:
new_password = input("Please enter your new password: ")
new_password2 = input("Please enter your new password again: ")
if new_password != new_password2:
print("The passwords don't match up.")
continue
elif len(new_password) < 8:
print("Your password must be longer")
continue
elif new_password.upper() == new_password or new_password.lower() == new_password:
print("Your password must contain at least one lowercase and uppercase letter")
continue
else:
print("Password Accepted!")
valid_password = True
Hope this helps!
a= int (input("Enter Passcode: "))
if a == 1974:
print (" Welcome!! ")
else:
print ("Wrong Passcode")
print ("Run again")