name = input(str("Enter your firstname "))
surname = input(str("Enter your surname "))
username = surname[0:2] name[0:3]
password = input("Make a password ")
passwordconfirm = input("Enter password again ")
while password != passwordconfirm:
print("Password is not the same try again ")
password = input("Make a password ")
passwordconfirm = input("Enter password again ")
print("This is your username and password:\n" username "\n" password)
attempt = 0
login1 = input("Enter your username ")
login2 = input("Enter your password ")
while True:
if login1 == username and login2 == password:
print("Welcome back " name " " surname)
break
else:
print("Access denied")
attempt = 1
#When I run the program and username or password is incorrect it starts to infinitely print "Access Denied"
CodePudding user response:
break breaks out of a loop, not an ifstatement or function as others have mentioned above. Your program will stop running itself if conditions are matched.
if login1 == username and login2 == password:
print("Welcome back " name " " surname)
else:
print("Access Denied.")
CodePudding user response:
Alright, I think the code needs a few more steps to make it work. If I understand it well, you want the user to sign up for a new account using username and password. Then, you need to verify it to see if it's correct.
Here is a sample:
#*********** BEGIN ACCOUNT CREATION ***************
msg = "Invalid entry. Try again."
print("Welcome to Python Account!")
while True:
# ***** THIS BLOCK CREATES NEW USERNAME
try:
username = input("Create New Username: ") #create new username
except ValueError:
msg
else:
try:
username2 = input("Confirm username: ") # confirm username
except ValueError:
msg
else:
if username2 != username: # if no match, try again
print("Usernames do not match. Try again.")
continue
else:
print("Username created!")
#THIS BLOCK CREATES NEW PASSWORD
while True:
try:
password = input("Create a new password: ")
except ValueError:
msg
else:
password2 = input("Confirm password: ")
if password2 != password:
print("Passwords don't match. Try again.")
continue
else:
print("Password created!")
break
break
break
# ****************** END ACCOUNT CREATION ******************
# # ****************** BEGIN ACCOUNT LOGIN *****************
print("PLEASE LOG INTO YOUR ACCOUNT")
attempts = 5 # this will allow 5 attempts
while attempts > 0:
attempts -= 1
try:
usrnm = input("Enter username: ")
pwd = input("Enter password: ")
except ValueError:
msg
else:
if usrnm.isspace() or pwd.isspace():
print("Space not allowed.")
continue
elif usrnm == '' or pwd == '':
print("Value needed.")
continue
elif usrnm != username2 or pwd != password2:
print("Credentials do not match. Try again.")
print(f'You have {attempts} attempts left')
if attempts == 0: # once user exhausts attempts, account is locked
print("You have been locked out of your account")
break
continue
else:
print(f"Welcome {username2}!")
break
break
# *************** END ACCOUNT CREATION *************
WITHOUT TRY/EXCEPT
#*********** BEGIN ACCOUNT CREATION ***************
msg = "Invalid entry. Try again."
print("Welcome to Python Account!")
while True:
# ***** THIS BLOCK CREATES NEW USERNAME
username = input("Create New Username: ") #create new username
username2 = input("Confirm username: ") # confirm username
if username2 != username: # if no match, try again
print("Usernames do not match. Try again.")
continue
else:
print("Username created!")
#THIS BLOCK CREATES NEW PASSWORD
while True:
password = input("Create a new password: ")
password2 = input("Confirm password: ")
if password2 != password:
print("Passwords don't match. Try again.")
continue
else:
print("Password created!")
break
break
# ****************** END ACCOUNT CREATION ******************
# # ****************** BEGIN ACCOUNT LOGIN *****************
print("PLEASE LOG INTO YOUR ACCOUNT")
attempts = 5 # this will allow 5 attempts
while attempts > 0:
attempts -= 1
usrnm = input("Enter username: ")
pwd = input("Enter password: ")
if usrnm.isspace() or pwd.isspace():
print("Space not allowed.")
continue
elif usrnm == '' or pwd == '':
print("Value needed.")
continue
elif usrnm != username2 or pwd != password2:
print("Credentials do not match. Try again.")
print(f'You have {attempts} attempts left')
if attempts == 0: # once user exhausts attempts, account is locked
print("You have been locked out of your account")
break
continue
else:
print(f"Welcome {username2}!")
break
# *************** END ACCOUNT CREATION *************
CodePudding user response:
if and else is not a type of loop you should use return instead of break
if login1 == username and login2 == password:
print("Welcome back " name " " surname)
return # the code stops here if the condition is true
else:
print("Access Denied.")
