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
Related
well I'm getting this error when I try to use login option
"local variable 'register' referenced before assignment"
def logins():
choice = int(input("1) Do you want to Login?: \n2) Do you want to Register?: \n"))
if choice == int(2):
info = input("choose UserName: \n")
password = input("choose PassWord: \n")
register = open("D:\login\Login.txt", "r+")
register.write(info + "\n" + password)
if choice == int(1):
infos = input("choose UserName: \n")
passwords = input("choose PassWord: \n")
if register == str(infos):
print("Welcom Back! ")
#register.close()
logins()
I think this code may help you.
As you use register variable in both case, then you can defined this variable outside the if statement.
def logins():
choice = int(input("1) Do you want to Login?: \n2) Do you want to Register?: \n"))
register = open("D:\login\Login.txt", "r+")
if choice == 2: # 2 is already int no need to change it to int.
info = input("choose UserName: \n")
password = input("choose PassWord: \n")
register.write(info + "\n" + password)
if choice == 1: # 1 is already int no need to change it to int.
infos = input("choose UserName: \n")
passwords = input("choose PassWord: \n")
if register == str(infos):
print("Welcom Back! ")
register.close()
logins()
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
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