How to make long "if" statement? - python

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

Related

How do i get this code to work? login/register program in python with dictionary and functions

login_input = {"MEK1300":"Python"}
username = {}
user = input("To login enter Yes. If you want to register enter No: ")
def login_info():
if user == "Yes":
login_user()
else:
user == "No"
register_user()
return user
def register_user():
new_user = input("Enter a username: ")
if new_user in login_input:
print("Your username already exists! ")
else:
new_password = input("Enter a password: ")
username[new_user] = new_password
print("Successful registration!")
def login_user():
login_user = input("Enter your username: ")
password = input("Enter your password: ")
if login_user in login_input and login_input[login_user] == password:
print("Successful login! ")
else:
print("Invalid username/passord. Register a new user!")
This is the beginning of a multiple choice quiz in python btw. How can i make this work? I dont want it to be to complicated.
I think youre code is fine you need to call you first fucnction to begin the quiz with login_info()
Updated code:
login_input = {"MEK1300":"Python"}
username = {}
user = input("To login enter Yes. If you want to register enter No: ")
def login_info():
if user.upper() == "YES": #Convert YeS to YES
login_user()
else:
register_user()
return user
def register_user():
new_user = input("Enter a username: ")
if new_user in login_input:
print("Your username already exists! ")
else:
new_password = input("Enter a password: ")
username[new_user] = new_password
print("Successful registration!")
def login_user():
login_user = input("Enter your username: ")
password = input("Enter your password: ")
if login_user in login_input and login_input[login_user] == password:
print("Successful login! ")
else:
print("Invalid username/passord. Register a new user!")
login_info()
You did define a login_info() function but didn't really call it in the global scope. Also, the username dictionary wouldn't be persistent. After your first run, the username will be an empty dictionary again, so the register is not working. You can try saving the username dictionary as a JSON file and reading the file when you need to rerun it.

python how do i break an infinite loop without ending a while statement? [duplicate]

This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 4 months ago.
Hi I have been looking for an answer to this question for my lab and I can't seem to find it on here. If it's been asked already I apologize. With that said I am making a password vault menu and I am stuck on the very last section (the else statement at the bottom) of the lab. If the user gives an unknown input I am supposed to print what is in my attached code and he specifies to continue the loop. The clear solution to the infinite loop is a break statement but that makes additional inputs not work so if anyone could help me allow the user to provide an input without copying all of that again for the else I would greatly appreciate it.
else:
print(" ======Menu======")
print("1. View Password Vault")
print("2. Add a new credential")
print("3. Update an existing credential")
print("4. Exit")
menu_item = input("\nChoose a menu item: ")
menu_item = True
while menu_item == True:
if menu_item == "1":
print(password_vault)
elif menu_item == "2":
website_name = input("What site: ")
email_address = input("What email: ")
password = input("What password: ")
validate(email_address, password)
password_vault[website_name.title()] = {email_address:password}
elif menu_item == "3":
website_name = input("What site: ")
email_address = input("What email: ")
password = input("What password: ")
if website_name in password_vault:
validate(email_address, password)
password_vault[website_name.title()] = {email_address:password}
print(password_vault)
if menu_item == "4":
menu_item = False
else:
print("Unknown value entered. Please try again!")
break
This is what you are looking for.
while True:
print(" ======Menu======")
print("1. View Password Vault")
print("2. Add a new credential")
print("3. Update an existing credential")
print("4. Exit")
menu_item = input("\nChoose a menu item: ")
if menu_item == "1":
print(password_vault)
elif menu_item == "2":
website_name = input("What site: ")
email_address = input("What email: ")
password = input("What password: ")
validate(email_address, password)
password_vault[website_name.title()] = {email_address:password}
elif menu_item == "3":
website_name = input("What site: ")
email_address = input("What email: ")
password = input("What password: ")
if website_name in password_vault:
validate(email_address, password)
password_vault[website_name.title()] = {email_address:password}
print(password_vault)
elif menu_item == "4":
break
else:
print("Unknown value entered. Please try again!")

Function that iterates over text file

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.

My code keeps printing a certain part of the code, instead of continuing with the rest of the program [duplicate]

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

Python account system

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

Categories