i have tried
def main():
while True:
UserName = input ("Enter Username: ")
PassWord = input ("Enter Password: ")
if UserName == Bob and PassWord == rainbow123:
time.sleep(1)
print ("Login successful!")
logged()
else:
print ("Password did not match!")
def logged():
time.sleep(1)
print ("Welcome to ----")
main()
but i dont know why it wont work
somebody plz help
The issue is that you haven't defined the username and password as a string in the 'if' statement.
import time
def main():
while True:
UserName = input("Enter Username: ")
PassWord = input("Enter Password: ")
if UserName == "Bob" and PassWord == "rainbow123":
time.sleep(1)
print("Login successful!")
logged()
else:
print("Password did not match!")
def logged():
time.sleep(1)
print("Welcome to ----")
main()
Related
import hashlib
def signup():
email = input("Enter an email address: ")
pwd = input("Enter a password: ")
conf_pwd = input("Confirm your password: ")
if conf_pwd == pwd:
enc = conf_pwd.encode()
hash1 = hashlib.sha256(enc).hexdigest()
with open(r'D:\Python Programs\Login\database.txt', "a") as f:
f.write(email + "\n")
f.write(hash1 + "\n")
f.close()
print("You have registered successfully!")
else:
print("Password is not same as above! \nPlease try again")
signup()
def login():
email = input("Enter email: ")
pwd = input("Enter password: ")
auth = pwd.encode()
auth_hash = hashlib.sha256(auth).hexdigest()
with open(r'D:\Python Programs\Login\database.txt', "r") as f:
stored_email, stored_pwd = f.read().split("\n")
f.close()
if email == stored_email and auth_hash == stored_pwd:
print("Logged in Successfully!")
else:
print("Login failed! \nPlease try again")
login()
def welcome():
print("Welcome to the [Insert Game Name Here]")
HaveAccount = input("Have you made an account before? (Y/N): ")
if HaveAccount == ("Y"):
login()
elif HaveAccount == ("N"):
signup()
else:
print("Please enter either 'Y' or 'N'")
welcome()
welcome()
It is specifically line 23 and it has a ValueError: too many values to unpack (expected 2) whenever I try and log in with an email and password. I need to add some extra text in because my post is mostly code and I don't know what else to say so here is some random typing to make more characters in my post. Any help is appreciated. Thanks
I'm making a simple menu for students to see and admins to login.
def main():
while(True):
print("1. Display Class")
print("2. Admin Login")
a = input("Please enter a choice or q to quit: ")
if a=="1":
Q1()
elif a=="2":
Q2()
elif a=="q":
break
def Q1():
print('Class A in Room 1, Class B in Room 2')
def Q2():
passwd = {'admin1': Q2}
user_input = input("Enter administrator password: ")
if user_input in passwd:
func = passwd[user_input]
func()
print ('Success')
else:
print("Incorrect password")
main()
print("Goodbye")
The Q2() function does not work as I intended. It keeps looping "Enter administrator password: " when password is entered correctly.
Correct?
def Q2():
passwd = {'admin1','admin2','admin3'}
user_input = input("Enter administrator password: ")
if user_input in passwd:
print ('Success')
else:
print("Incorrect password")
def register():
print("Register")
login = input("Enter login")
passwd = input("Enter password")
passwdacc = input("Accept password")
if passwdacc = passwd:
print("You have registered, now please sign up")
else:
print("Try again, passwords dont matches")
login2 = input("Enter login")
password = input("Enter password")
if password = passwd and login2 = login
print("Accepted")
else:
print("Try again")
sign_up()
Error(s), warning(s):
File "source_file.py", line 3
login = input("Enter login")
^
IndentationError: unindent does not match any outer indentation level
Apart from wrong indentation, in conditional statements you need to replace assignment operators with comparison operators. Also, a : is missing from an if statement.
def register():
print("Register")
login = input("Enter login")
passwd = input("Enter password")
passwdacc = input("Accept password")
if passwdacc == passwd:
print("You have registered, now please sign up")
else:
print("Try again, passwords don't match")
login2 = input("Enter login")
password = input("Enter password")
if password == passwd and login2 == login:
print("Accepted")
else:
print("Try again")
def register():
print("Register")
login = input("Enter login")
passwd = input("Enter password")
passwdacc = input("Accept password")
if passwdacc = passwd:
print("You have registered, now please sign up")
else:
print("Try again, passwords dont matches")
login2 = input("Enter login")
password = input("Enter password")
if password = passwd and login2 = login
print("Accepted")
else:
print("Try again")
So I've been trying to create a quiz where you have to have an account which means you can register and login. I managed to code the register part(which i'm pretty proud of) and it saves the login details to a separate text file, when it saves the login details it looks like this: username:password
Now i'm struggling with the login part, I think you have to read the text file and then split the username and password, then I some how have to compare the inputted username and password to the saved ones.
This is what I done for the login part so far but it doesn't work:
def login():
filename = 'Accounts.txt'
openfile = open('Accounts.txt', "r")
Userdata = openfile.readlines()
with open('Accounts.txt', 'r') as file:
for line in file:
user2, passw = line.split(':')
login2 = input("Enter username: ")
passw2 = input("Enter passwordd: ")
if login2 == user2 and passw2 == passw:
print("Logged in")
else:
print("User or password is incorrect!")
openfile.close();
Now this is how the whole code looks like(if needed):
import time
print("Welcome to my quiz")
#Creating username
def create():
print ("We will need some information from you!")
time.sleep(1)
Veri = input("Would you like to continue (yes or no): ")
def createAccount():
while True:
name = input("Enter your first name: ")
if not name.isalpha():
print("Only letters are allowed!")
else:
break
while True:
surname = input("Enter your surname: ")
if not surname.isalpha():
print("Only letters are allowed!")
else:
break
while True:
try:
age = int(input("Enter your age: "))
except ValueError:
print("Only numbers are allowed!")
continue
else:
break
if len(name) >= 3:
username = name[0:3]+str(age)
elif len(surname) >= 3:
username = surname[0:3]+str(age)
else:
username = input("Create a username: ")
print ("Your username is:",username)
while True:
password = input("Create a password: ")
password2 = input("Confirm your password: ")
if password != password2:
print("Password does not match!")
else:
break
account = '%s:%s\n'%(username,password)
with open ('Accounts.txt','a') as file:
file.write(account)
print ('Account saved')
if Veri == 'no':
menu()
elif Veri == 'yes':
createAccount()
else:
print ("Sorry, that was an invalid command!")
time.sleep(1)
login()
#Loging in
def login():
filename = 'Accounts.txt'
openfile = open('Accounts.txt', "r")
Userdata = openfile.readlines()
with open('Accounts.txt', 'r') as file:
for line in file:
user2, passw = line.split(':')
login2 = input("Enter username: ")
passw2 = input("Enter passwordd: ")
if login2 == user2 and passw2 == passw:
print("Logged in")
else:
print("User or password is incorrect!")
openfile.close();
time.sleep(1)
#Choosing to log in or register
def menu():
LogOrCre = input("Select '1' to login or '2' to register: ")
if LogOrCre == '1':
login()
elif LogOrCre == '2':
create()
else:
print ("Sorry, that was an invalid command!")
menu()
menu()
If you have any ideas on how I can make the login part, that would be helpful.
You are asking for the user's username and password for every line in the accounts file. Get the inputted login information outside of the for loop.
Welcome to programming!
It can be very daunting, but keep studying and practicing. You are in the right way!
I see some points in your code that could be improved. I'm commenting below:
1) You don't need to use both open() (and close()) and with to access a file's contents. Just use with, in this case. It will make you code simpler. (A good answer about with)
2) You're asking for the user login & passwd inside a loop (aka multiple times). It can be very annoying for your user. Move the input's call to before the for loop.
3) You also need to break the loop when the login succeeds.
So, a slightly improved version of your code would be:
filename = 'Accounts.txt'
with open(filename, 'r') as file:
login2 = input("Enter username: ")
passw2 = input("Enter password: ")
for line in file:
user2, passw = line.split(':')
if login2 == user2 and passw2 == passw:
print("Logged in")
break
else:
print("User or password is incorrect!")
Not sure what is not working in your code, but I updated the code as below and was able to print logged in.
def login():
#filename = 'Accounts.txt'
#openfile = open('Accounts.txt', "r")
#Userdata = openfile.readlines()
with open('Accounts.txt', 'r') as file:
login2 = input("Enter username: ")
passw2 = input("Enter passwordd: ")
for line in file:
user2, passw = line.split(':')
if login2 == user2 and passw2 == passw:
print("Logged in")
break
else:
continue
login()
import random
import time
print ("Welcome to 'Project X'")
print ("----------------------")
login = input("Please enter your username:")
time.sleep(1)
print ("Please wait...")
time.sleep(1)
print ("Generating randomized password")
password = random.randint(1000000000, 9000000000)
time.sleep(3)
print ("This is your randomized password:", password)
print ("Write down your password, you have 10 seconds")
time.sleep(10)
import os
os.system("cls")
login2 = input("Please enter your username:")
if login2 == login:
password2 = input("Please enter your password:")
if password2 == password:
print ("Access Granted, Welcome Mr.", login)
time.sleep(2)
ipaddress = input("Enter an ip address you want to ping:")
print ("Processing ip address...")
time.sleep(2)
os.system("ping ", ipaddress)
elif password2 != password:
print ("Access Denied")
print ("Sending S.W.A.T Team")
time.sleep(2)
print ("...")
time.sleep(2)
print ("Shutting down systems...")
os.system("cls")
else:
print ("Access Denied")
print ("Sending S.W.A.T Team")
time.sleep(2)
print ("...")
time.sleep(2)
print ("Shutting down systems...")
os.system("cls")
I want it find out why it doesn't run the rest of the code when it comes to entering the password again, even though its right it still says access denied and goes through the process of the other commands.
You have the following line generate an int
password = random.randint(1000000000, 9000000000)
Then you ask for a str input with
password2 = input("Please enter your password:")
This comparison will always be false
if password2 == password:
# ^str ^int
You have to compare the same type, so for example
if int(password2) == password:
# ^int ^int
Or convert the input on the way in
password2 = int(input("Please enter your password:"))
random.randint(1000000000, 9000000000) returns an int. input("Please enter your password:") returns a str. Use int(input("Please enter your password:")).