I'm trying to practice because I'm doing Not Examined Assessment in school and I'm stuck with my code, it doesn't work as intended because it doesn't matter whatever I write in accnick it says that the username is correct :/
acc = input("Do you have an existing account? y/n ")
if acc == "y":
accnick= input("Enter your username. ")
while accnick in open("file.txt").read():
print("Correct username.")
accpass= input("Enter your password. ")
if len(accpass) == 0:
accpass = input("Try again. ")
while accpass in open("file.txt").read():
print("Correct password. ")
break
else:
accnick = input("Wrong username, try again. ")
elif acc == "n":
name = input("Enter your name. ")
while len(name) == 0:
name = input("You haven't entered anything, try again. ")
age = int(input("Enter your age. "))
age = str(age)
while len(age) == 0:
age = input("Enter your age again. ")
password = input("Enter your password. ")
nick = name[:3]
nickname = nick+str(age)
file=open("file.txt","w+") #w+ is to write to a file and create a file if it doesnt exist yet
file.write(name+' '+str(age)+' '+nickname+' '+' '+password)
file=open("file.txt","r")
kurwa=file.read()
print(kurwa)
name = input("Enter your name. ") ###
if 'Test Test' in open('file.txt').read():
print("Someone with that name already exists. ")
In your code it will jumpback to print("Correct username.") after a correct username was typed in. So you are stuck in a endless loop. Your break should be part of the while loop. Further I changed the second while to if.
Try this:
acc = input("Do you have an existing account? y/n")
if acc == "y":
accnick= input("Enter your username. ")
while accnick in open("file.txt").read():
print("Correct username.")
accpass= input("Enter your password. ")
if len(accpass) == 0:
accpass = input("Try again. ")
if accpass in open("file.txt").read():
print("Correct password. ")
break
Related
I'm a student who is doing my Python assignment. Thing is, our lecturer is really strict on us not using python built ins and also refuses to check our code before submission and I'm having the hardest time trying to get my code to work properly during registration.
My problem currently is that I want the code to prompt an error from the program itself when the user tries to register with a taken username. However, it keeps running different errors regardless of how I try to fix it. Can anyone please point me in the right direction or tell me what's wrong with my code? I'd be really grateful for any help.
def grant():
print("Helllooooo")
def begin():
while True:
print("Welcome to the Freshco Groceries App!")
option = input("Are you a new or returning user?\n"
"Enter 1 if you would like to LOGIN\n"
"Enter 2 if you would like to register\n"
"Enter 3 if you would like to exit the program\n")
if option=="1":
access(option)
break
elif option=="2":
access(option)
break
elif option=="3":
print("Thank you! Have a good day.")
exit()
else:
continue
def access(option):
if(option=="1"):
name = input("Please enter your username:\n")
password = input("Please enter your password:\n")
login(name,password)
else:
print("First step: Please choose a username and a unique password.")
name = input("Please enter your username:\n")
password = input("Please enter your password:\n")
register(name,password)
newuser()
def login(name,password):
success=False
file = open("user_details.txt","r")
for line in file:
a,b = line.split(",")
if (name in a) and (password in b):
success=True
break
file.close()
if(success):
print("You have successfully logged in! Happy shopping!")
grant()
else:
print("Wrong username or password entered.")
def register(name,password):
exist = False
while True:
file = open("user_details.txt", "r")
for line in file:
line = line.rstrip()
if (name == line):
exist = True
break
else:
pass
file.close()
if (exist):
print("Username has been taken. Please try again with a new one.")
break
else:
file = open("user_details.txt","a")
file.write("\n" + name + "," + password)
print("You have successfully registered! Welcome to the Freshco Family!")
grant()
break
def newuser():
print("Before you start using the app, please fill in the details below.")
alldata = []
while True:
newdata = []
nname = input("Please enter your full name:\n")
newdata.append(nname)
while True:
ngender = input("Are you female or male?\n"
"1: female\n"
"2: male\n")
if ngender=="1":
fingender="female"
newdata.append(fingender)
break
elif ngender=="2":
fingender="male"
newdata.append(fingender)
break
else:
print("INVALID INPUT")
continue
naddress = input("Please enter your address:\n")
newdata.append(naddress)
nemail = input("Please enter your email address:\n")
newdata.append(nemail)
ncontact = input("Please enter your contact number:\n")
newdata.append(ncontact)
ndob = input("Please enter your dob in this format: <DD.MM.YY>\n")
newdata.append(ndob)
alldata.append(newdata)
print(newdata)
break
print("Thank you for entering your information, you will be redirected now!")
grant()
begin()
thank you so much! T-T
So I want to make a login page but indentation is getting in the way, I'm new to Python and whatever I do seems to keep messing it up.
if StartingPrompt == "l":
username_2 = input("Enter your username: ")
password = input("Enter your password: ")
else:
print("Please only enter 's' or 'l' ")
return StartingPrompt
Your indentation was messed up.
if StartingPrompt == "l":
username_2 = input("Enter your username: ")
password = input("Enter your password: ")
else:
print("Please only enter 's' or 'l' ")
Fix your indentation (near the password,else,return):
def YOUR_FUNCTION():
if StartingPrompt == "l":
username_2 = input("Enter your username: ")
password = input("Enter your password: ")
else:
print("Please only enter 's' or 'l' ")
return StartingPrompt
Adding comments for clarity.
if StartingPrompt == "l":
username_2 = input("Enter your username: ")
password = input("Enter your password: ") # one tab indent here
else: # one un-indent here
print("Please only enter 's' or 'l' ")
return StartingPrompt
I don't know if you are use to programming but try to think python indent as the () in other language. Indent in python is the same as layer. You need to stay at the same layer unless you have finished your statement.
if StartingPrompt == "l":
username_2 = input("Enter your username: ")
password = input("Enter your password: ") #you can t go down because your if statement isn't over
else: #else statement of a given if statment is at the same level of the if statement
print("Please only enter 's' or 'l' ")
Assuming that you initialize StartingPrompt somewhere else, your code should look as follows:
if StartingPrompt == "l":
username_2 = input("Enter your username: ")
password = input("Enter your password: ")
else:
print("Please only enter 's' or 'l' ")
After every if statement, you need an indented block; the same goes for def(functions), try-except, etc.
For example:
if <condition>:
# indent
print("Inside if-body.")
elif <condition>:
# indent
print("Inside elif-body.")
else:
# indent
print("Inside else-body.")
# back to original indentation
print("Outside of if-statment.")
You also have a return statement, but you do not need this as your code is not contained within a function. If you would like to use a function:
def login():
# indent here (inside function body)
StartingPrompt = input("Please enter option: ")
if StartingPrompt == "l":
username_2 = input("Enter your username: ")
password = input("Enter your password: ")
else:
print("Please only enter 's' or 'l' ")
return StartingPrompt
Use 4 spaces for indentation. Read PEP 8 for a more styling guide.
if StartingPrompt == "l":
username_2 = input("Enter your username: ")
password = input("Enter your password: ")
else:
print("Please only enter 's' or 'l' ")
return StartingPrompt
I'm a total newbie to programming and have just begun my first project which is a simple login app.
So far my code looks like this:
def login_or_registration():
user_choice = input("Login (L) or Registration (R)? ").lower()
if user_choice == "l":
print("You will be redirected to the login window.")
login()
elif user_choice == "r":
print("You will be redirected to the registration window.")
registration()
else:
print("Invalid choice.")
def registration():
name = input("Name: ")
surname = input("Surname: ")
username = input("Username: ")
pass1 = input("Password: ")
pass2 = input("Repeat password: ")
if pass1 != pass2:
print("Passwords don't match!")
email = input("E-mail: ")
age = input("Age: ")
while True:
if not age.isdecimal():
print("Please enter a number.")
if not name.isalpha():
print("Please use only letters for Name and Surname.")
if not surname.isalpha():
print("Please use only letters for Name and Surname.")
break
with open("userbase.txt", "a") as f:
f.write(username + "\n" + pass1 + "\n" + surname + "\n" + name + "\n" + email + "\n" + age + "\n" + "\n")
f.close()
user_choice = input("Would you like to log in now? (Y/N) ").lower()
if user_choice == "y":
login()
else:
"Quitting."
def login():
logged_in = False
username = input("USERNAME: ")
password = input("PASSWORD: ")
while logged_in == False:
f = open("userbase.txt", "r")
for line in f:
if line == username:
logged_in = True
print("Logged in!")
print("User not found!")
login_or_registration()
I know its incomplete and messy but for now i want to work on a function for logging in. My idea was to create a function that would iterate over a text file and search for a match to the users input I know it's not perfect and could have many problems but for now I want to avoid modules. I wrote the function login() but it doesn't work and I don't have an idea how to make it run. My idea was to make an if statement that would be checked for every iteration of a for loop inside a while loop combined with a boolean that would be set to True after the statement is true.
I am trying to make a long if statement where it asks you for a sign up or login, but when I get to the login part there is a syntax error. Any tips?
registration = input("Do you have a registration")
if registration == "No":
name = input("Type your name: ")
surname = input("Type your surname: ")
userp1 = name[0]+ surname.capitalize()
print(userp1)
password = input("Enter your password\n")
userInput = input("Type your login details\n")
if userInput == userp1:
userInput = input("Password?\n")
if userInput== password:
print("Welocome")
change = input("Do you want to change your username?")
if change == "No":
print("You logged in as" , userp1)
else:
userp1 = input("What would your new username be?")
print("You logged in as",userp1)
else:
print("Login")
You are writing two else statement consecutively in the last lines, and it is invalid syntax. You can put an if statement in another if statement, and you have to. This is working code but I am not sure if it is what you trying to make:
registration = input("Do you have a registration")
if registration == "No":
name = input("Type your name: ")
surname = input("Type your surname: ")
userp1 = name[0]+ surname.capitalize()
print(userp1)
password = input("Enter your password\n")
userInput = input("Type your login details\n")
if userInput == userp1:
userInput = input("Password?\n")
if userInput== password:
print("Welocome")
change = input("Do you want to change your username?")
if change == "No":
print("You logged in as" , userp1)
else:
userp1 = input("What would your new username be?")
print("You logged in as",userp1)
else:
print("Login")
Your code is not well indented. Be aware that python is sensetive to indentation.
You also don't specify what is the error you are getting.
So I take the liberty and tried to do a code that matches at maximum to your code.
Here it is:
registration = input("Do you have a registration")
if registration == "No":
name = input("Type your name: ")
surname = input("Type your surname: ")
userp1 = name[0]+ surname.capitalize()
print(userp1)
password = input("Enter your password\n")
userInput = input("Type your login details\n")
if userInput == userp1:
userInput = input("Password?\n")
if userInput== password:
print("Welocome")
change = input("Do you want to change your username?")
if change == "No":
print("You logged in as" , userp1)
else:
userp1 = input("What would your new username be?")
print("You logged in as",userp1)
else:
print("Login")
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 3 years ago.
After I type in any username and password (doesn't matter whether it's correct or not), it keeps printing another part of the code.
The login part works fine but it doesn't show the correct output afterwards. It continuously shows:
"Incorrect login details entered
Have you made an account?
Yes or No"
This has stumped both my teacher and I. I have looked at different websites with examples of login/registration systems. I have also tried re-arranging the code differently.
This is the code:
username = input("Please enter your username: ")
password = input("Please enter your password: ")
file = open("Usernames.txt","r")
found = False
for line in file:
user = line.split(",")
if user[0] == username and user[1] == password:
found = True
print("Username: " + user[0])
print("~~~~~")
print("Welcome to the game, " + user[0])
else:
found == False
print("Incorrect login details entered")
print("Have you made an account?")
ans = input("Yes or No ")
while ans not in ("Yes", "No"):
if ans == "Yes":
print ("Please sign in again")
username = input("Please enter your correct username: ")
password = input("Please enter your correct password: ")
elif ans == "No":
print("Would you like to make an account? ")
else:
ans = input("Please enter Yes or No ")
The expected result when the username and password is correct:
Username: Smartic
~~~~~
Welcome to the game, Smartic
The expected result when the username and password is incorrect:
Incorrect login details entered
Have you made an account?
Yes or No
The expected result when the user enters Yes:
Please sign in again
Please enter your correct username:
Please enter your correct password:
The expected result when the user enters No:
Would you like to make an account?
The expected result when the user enters something other than Yes or No:
Please enter Yes or No
No matter what username you enter, it won't satisfy every username in the file. Every time it doesn't, your error text will be printed. Reformat your code as is described in this question.
There is a new line at the end of each line in the file. You can remove newline by using strip() function.
if user[0] == username and user[1].strip() == password:
found = True
print("Username: " + user[0])
print("~~~~~")
print("Welcome to the game, " + user[0])
Change your:
while ans not in ("Yes", "No"):
In:
while True:
Besides I can recommend to make a function.
Also as John Gordon mentioned use breaks, so your script will look like:
username = input("Please enter your username: ")
password = input("Please enter your password: ")
user = {0:'e',1:'w'}
found = False
if user[0] == username and user[1] == password:
found = True
print("Username: " + user[0])
print("~~~~~")
print("Welcome to the game, " + user[0])
else:
found == False
print("Incorrect login details entered")
print("Have you made an account?")
ans = input("Yes or No ")
while True:
if ans == "Yes":
print ("Please sign in again")
username = input("Please enter your correct username: ")
password = input("Please enter your correct password: ")
break
elif ans == "No":
print("Would you like to make an account? ")
break
else:
ans = input("Please enter Yes or No ")
break