When I enter my username, "good job" isn't being displayed - python

import time
def Scenario2():
film_comedy = ['Anchorman','The Hangover','Ted']
film_horror = ['The Exorcist','The Shining','Scream']
film_action = ['Die Hard','The Matrix','Batman']
film_animation = ['Toy Story','The Incredibles','The Lion King']
print("Hello, welcome to the online streaming service")
username = len(input("Please Enter a username between 4 and 12 characters: "))
while username < 4 or username > 12:
print("That username is not within the boundaries")
username = len(input("Please Enter a username between 4 and 12 characters: "))
password = input("Now enter a password: ")
password1 = input("Please re-enter the password: ")
if password1 == password:
print("Congratualtions on your new account")
while password1 != password:
print("They don't match")
password = input("Now enter a password: ")
password1 = input("Please re-enter the password: ")
if password1 == password:
print("Congratualations on your new account")
usernameinput = input("Please Enter your username: ")
if usernameinput == username:
print("Great job")
while usernameinput != username:
print("That is the incorrect Username")
usernameinput = input("Please Enter your username: ")
if usernameinput == username:
print("Great job")
Scenario2()
Whenever I enter the usernameinput the same as username. it doesn't say good job but that it is incorrect.
Could I get some help with this?

while usernameinput != username:
print("That is the incorrect Username")
usernameinput = input("Please Enter your username: ")
if usernameinput == username:
print("Great job")
This won't work because, you have assigned username = len(input("Please Enter a username between 4 and 12 characters: ")) before this while loop, rename your variables for this to work.

Username is being assigned as the length of the input, not the input. You need to introduce a separate variable to assign the input too.

Related

Log in is always saying it is incorrect [duplicate]

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.

How to check if input is same as item in list after the first input is wrong?

I was trying to create a login system that will check if the username and password is in the list or not. The problem that I have with the code below is that when the first array of credentials does not match, it will checks the first user-password pair only for the next input. May I know what's the reason for this error and how can I avoid it ?
userlist = [["k",'l'],['m','z'],['l','o']] #dummy data
def signin():
username = input("Please enter username: ")
for user in userlist:
if username == user[0]:
password = input("Please enter password: ")
if password == user[1]:
print("correct password")
signin()
else:
print("Incorrect password")
signin()
else:
print("Unregister username")
signin()
signin()
Output:
Please enter username: k
Please enter password: l
correct password
Please enter username: m
Unregister username
Please enter username: l
Unregister username
Please enter username:
Expecting output:
Please enter username: k
Please enter password: l
correct password
Please enter username: m
Please enter password: z
correct password
Please enter username: l
Please enter password: o
correct password
Please enter username:
Back space your last else so that it is inline with your for loop. Why? Well, right now if you enter l at the first input, it will check this list ["k",'l']. It is not there so it will go to the else. Putting the else outside the for loop makes sure we iterate through the entire list before making a decision.
userlist = [["k",'l'],['m','z'],['l','o']] #dummy data
def signin():
username = input("Please enter username: ")
for user in userlist:
if username == user[0]:
password = input("Please enter password: ")
if password == user[1]:
print("correct password")
signin()
else:
print("Incorrect password")
signin()
else:
print("Unregister username")
signin()
signing()

How can i sipmlify my code(Python login system)?

I'm a new coder. And this is my first login system code with python. How can i simplify my code without losing any functions like wrong username and wrong password etc.?
username = "zaphod"
password = "helloworld42"
username2 = "mozzie"
password2 = "mozzietheaussie"
userUsername = input("Hello, What is your username? \n")
UserPassword = input(print("Hello", userUsername, "What is your password? "))
if userUsername == username:
if password == UserPassword:
print("Hello", userUsername, "Welcome home")
if userUsername == username:
if UserPassword != password:
print("Wrong Password")
if userUsername == username2:
if UserPassword != password2:
print("Wrong Password")
if UserPassword == password:
if userUsername != username:
print("Wrong Username")
if UserPassword == password2:
if userUsername != username2:
print("Wrong Username")
if userUsername == username2:
if UserPassword == password2:
print("Hello", userUsername,"Welcome Home")
if userUsername == username:
if UserPassword == password2:
print("Are you gonna trick me pal xd")
if userUsername == username2:
if UserPassword == password:
print("Are you gonna trick me pal xd")
if userUsername != username:
if userUsername != username2:
if UserPassword != password:
if UserPassword != password2:
print("Wrong credidentals")
You can put the credentials in a dict and check if the user name exists and matches the password
credentials = {'zaphod': 'helloworld42',
'mozzie': 'mozzietheaussie'}
user_name = input('Hello, what is your username?\n')
password = input(f'Hello {user_name}, what is your password?\n')
pas = credentials.get(user_name) # returns None if the user name doesn't exists
if not pas:
print('Wrong Username')
elif pas != password:
print('Wrong Password')
else:
print(f'Hello {user_name}, welcome home')
The print("Are you gonna trick me pal xd") seems to be unnecessary, however you can add it by modifying the elif
elif pas != password:
if password in credentials.values():
print('Are you gonna trick me pal xd')
else:
print('Wrong Password')

Confirm user and password from a list of class objects

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.

python - Password checker program, while loops not working

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")

Categories

Resources