How starover a Python project? [duplicate] - python

This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 12 months ago.
I am doing a python project for my university and in the project I have a login information with user and password, but I want that if the user is wrong, the code repeats again and if the user is correct, the code pass and same case with password if password is wrong code starts from zero and if password is correct code continues with rest but i dont know how can i do this so please help and Thank you very much for the help.
User=input("Type the User: ")
if User=="Admin":
print("User correct")
Password=input("Type the password: ")
else:
print("User incorrect")
#In here if the user are incorrect, I need the firts starover
if Password=="admin":
print("Nice, continue")
else:
print("Wrong password")
#In here if the password are incorrect, I need the other starover.

You can use while to do this problem:
Loop until the username/password is correct.
Here is my solution:
User=""
Password=""
while(User != "Admin"):
User = input("Type the User: ")
if User=="Admin":
print("User correct")
else:
print("User incorrect")
#Do same thing with password
while(Password != "admin"):
Password=input("type password")
if Password=="admin":
print("Nice, continue")
else:
print("Wrong password")

Related

I want to know a form of a "Repeat until" block in scratch in python? [duplicate]

This question already has answers here:
Python: How to keep repeating a program until a specific input is obtained? [duplicate]
(4 answers)
Asking the user for input until they give a valid response
(22 answers)
Closed 10 months ago.
I need some help, you know those "repeat until" blocks in scratch? Well I need to know how to do that in python. What I want to do is this, with a repeat block.
retry = True
if password = "password1234":
retry = False
else:
pass
The following snippet checks if the password is "password1234". If it is correct, it changes the flag to False and hence the loop terminates. Otherwise, it will do further processing (e.g., ask the user for a new input).
retry = True
password = ""
while (retry):
# Check if the password equals to a specific password.
if (password == "password1234"):
retry = False
else:
# Do further processing here.
# Example: ask the user for the password
password = input("Enter a password: ")
Program will be asking you for password until you type password1234
password = input("Enter you password")
while password!="password1234":
password = input("Enter you password")
The above solutions are also correct. But if you want to keep your style, try this:
def get_password():
retry = True
password = input("Password: ")
if retry:
if password == "password1234":
retry = False
else:
return get_password()
get_password()

Storing input from user to txt file on Mac

So basically what this code is supposed to do is to be a login page. It asks wether if the user has an account or not ( in my case the user doesn't have an account ). The user creates an account ( this part works ) but the user info is not saved in the txt file. I looked at other sites and youtube videos that explain this topic, but none of them work. Visual Studio doesn't give me an error but when I open the txt file, the info is not shown. Also, I work on a mac ( don't know if it will matter ).
def main_menu():
OPTION = input("Do you have an account? (yes/no)\n")
if OPTION == "yes":
login()
if OPTION == "no":
register()
def register():
USERNAME = input("Create your username: \n")
PASSWORD = input("Create your password: \n")
database = open("databse.txt", "w")
database.write(USERNAME)
database.write(PASSWORD)
database.close()
PASSWORD_CHECK = input("Confirm your password: \n")
if PASSWORD_CHECK != PASSWORD:
print("Your passwords do not match. Try again. \n")
register()
if PASSWORD_CHECK == PASSWORD:
print("You have successfully created an account! Welcome!")
# another menu here
def login():
pass
main_menu()

My login system will not work - regardless of any user passwords entered

My sign up and login authentication system is not working as it is supposed to. My signup issue was fixed, but my login has a problem. Either the code will let me go through and access the account, or it will not, depending on the code. But anytime I try to fix it, the output is one of the two options. ALWAYS, regardless of the password I enter.
The usernames and passwords are stored in a txt file, like this:
John Appleseed:hisSuperSecretPassword
JohnDoe:1234
The login code:
found = False
username = input("Enter your username:\n")
file = open("account.txt", "r+")
for line in file:
if line.split(':')[0] == username:
found = True
if found == True:
password = input("Enter your password:\n")
for counter, line in enumerate(file):
if line.strip() == username + ":" + password:
print("You have signed in.")
else:
print("Password incorrect. Program closing.")
sys.exit()
else:
print("Username not valid.")
sys.exit()
Can anyone help? Running Python 3.9.2.
Here is something I've adjusted to work....
import sys
found = False
username = input("Enter your username:\n")
file = open("account.txt", "r+")
for line in file:
if line.split(':')[0] == username:
account_details = line.split(':')
found = True
if found == True:
password = input("Enter your password:\n")
if account_details[1].strip() == password:
print("You have signed in.")
else:
print("Password incorrect. Program closing.")
sys.exit()
else:
print("Username not valid.")
sys.exit()
You are exiting the program as soon as you find a non-matching password, instead of comparing the password to the correct user. You also don't need to re-read the entire password file: you already found the expected password when you verified that the user name existed.
As an aside, there's no sense confirming for an attacker that they have correctly guessed a user name. Just get the user name and password first, then look for them in the password file.
username = input("Enter your username:\n")
password = input("Enter your password:\n")
with open("account.txt") as fh:
if any(f'{username}:{password}' == line.strip() for line in fh):
print("You have signed in.")
else:
print("Invalid username or password, exiting")
sys.exit()

basic user log in/pw code

Just started learning python a couple days ago and have been trying to use what code I know to practice a basic code of asking for a user name and password from a list. I know there are far better/cleaner/matching user to password inputs but I'm just playing with what I know at this point.
users = ['Jon','Joe', 'Jole']
user_input = input('Username: ')
while user_input != users:
user_redo = input("I'm sorry but we dont recognize you. Please try another username: ")
this is where my problem is. Is there a simple way of breaking the loop if the user enters a matching username from the list?
passwords = ['donkey808','Hanna5006']
password = input('Password: ')
I guess the same question would apply to the password entry as well
while password != passwords:
pw_redo = input(f'Please enter correct password for user {user_input}: ')
else:
print(f'Access Granted {user_input}')
Write it like this.
users = ["Jon","Joe", "Jole"]
while 0 < 1 :
user_input = input('Username: ')
if user_input not in users:
print("I'm sorry but we dont recognize you. Please try another username: ")
elif user_input in users:
break
Just do while user_input not in users:
not in checks if user_input is literally not in users
It might also be better to do if user_input not in users:, I don't see the point of a while.

Using while loop to get the right username from user [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I want to create a code which will ask the user its username and password.
The username and password will already be a variable.
This code will keep asking the user the username and password over and over again
until the right username and password are typed in.
However I am not able to create this code, please help.
Can anyone give me an example, please?
I tried to create this code however, it doesn't work.First, if the username and the password are wrong the "Incorrect" just keeps repeating that's what I don't to happen and second I want that if the username or password is wrong the enter your username and enter your password keeps repeating until the user puts the credentials right.
answer_1 = ("america")
asnwer_2 = ("italy")
getin=input("Enter your Username: ")
getin_2=input("Enter your password :")
if getin!=answer_1 or getin_2!=answer_2:
print("Incorrect")
continue
print("Please proceed")
break
I fixed your code:
answer_1 = "america"
answer_2 = "italy"
getin_1 = input("Enter your Username: ")
getin_2 = input("Enter your password: ")
while getin_1 != answer_1 or getin_2 != answer_2:
print("Incorrect")
print("Please proceed")
getin_1 = input("Enter your Username: ")
getin_2 = input("Enter your password: ")
print("Your username and password are OK.")
To ask user again and again, you have to put question in a loop - if command is not sufficient.
There is no need to use break or continue commands as all required conditions are already in the while loop.
username = Bob
password = 123
user = ''
passw = ''
while username != user and password != passw:
passw = input('Input password:')
user = input('Input username:')

Categories

Resources