Q2() Function keeps looping - python

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

Related

How can i get my menu with password checker and write to file program working

Whenever I enter the password it keeps asking for a capital letter even when I enter it here is my code I think the function of checking the capital, numbers and length is not correct
import string
caps = string.ascii_uppercase
lowercase = string.ascii_lowercase
numbers = []
for i in range(10):
numbers.append(str(i))
def caps_found_pass(pw):
found = False
for letter in caps:
if pw.find(letter) > -1:
found= True
return found
def lowercase_found_pass(pw):
found = False
for letter in lowercase:
if pw.find(letter) > -1:
found= True
return found
def number_found_pass(pw):
found = False
for number in numbers:
if pw.find(number) > -1:
found = True
return found
def register ():
user_names_add = input ("Enter your first name and last name: ")
c = user_names_add.split ()
initial=c[0] [0]
lastname_initial=c [1] [0]
user_dob_add = input("Enter your Date Of Birth in format DDMMYYYY ")
username = initial + lastname_initial + user_dob_add
file = open("accounts.txt", "a")
file.write(username)
file.write(" ")
file.close()
print ("Your new username is ", username)
while True:
password = input("Please input your desired password: ")
if caps_found_pass(password):
if lowercase_found_pass(password):
if number_found_pass(password):
if len(password) > 6 and password < 15:
print(f'Your password is {password}')
pass
else:
print("Password must contain at least 7 characters and no more than 14 ")
else:
print("Password must contain a number")
else:
print("Password must contain a lowercase letter")
else:
print("Password must contain a capital letter")
file = open("accounts.txt")
file.write(password)
file.write("\n")
file.close()
def login ():
username = input("Please enter your username")
password = input("Please enter your password")
for line in open("accounts.txt","r").readlines():
login_info = line.split()
if username == login_info[0] and password == login_info[1]:
print("User and Pass correct logging in...")
return True
print("Incorrect User and/or pass.")
return False
def test():
open("accounts.txt")
def quit ():
print ("quitting")
def invalid ():
print ("invalid")
def user_menu ():
print ()
print("Menu")
print("-------------")
print()
print("Select A Function")
print("1. Login")
print("2. Register")
print("3. Testing/View all Users and Passwords")
print("9. Quit")
print()
selection = input ("Type, 1,2,3 or 9 ")
return selection
quit_program = False
while not quit_program:
option = user_menu()
if option == "1":
login ()
elif option == "2":
register ()
elif option == "3":
test ()
elif option == "9":
quit_program = True
else:
invalid ()
I have tried to change my function of checking the capital, numbers and length of the code but to no avail I believe the menu is working fine I just need the password to work so I can see if it can write to file

How do i make a password and username in trinket python?

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

Why won't my print statements show up in my elif block?

I know there is probably a simple solution to this, but the text I wrote under the elif sections in userName and passWord will not print when a user successfully logs in. Please help!
def userName():
username = "kate"
userInput = input('Please enter your username:\n')
while userInput != username:
if len(userInput) == 0:
print("Your username can not be blank, please try again!\n")
userInput = input('Please enter your username.\n')
elif userInput == username:
print("Welcome back, " + username)
print("")
break
else:
print("Sorry, that username is not in our system. Please try again!\n")
userInput = input('Please enter your username.\n')
def passWord():
password = "stags"
passInput = input("Please enter your password:\n")
while passInput != password:
if len(passInput) == 0:
print("Your password can not be blank, please try again!\n")
passInput = input("Please enter your password.\n")
elif passInput == password:
print("You have successfully logged in!\n")
break
else:
print("Sorry, your password is invalid. Please try again!")
passInput = input("Please enter your password.\n")
def main():
print("Hello, let's get started!")
print("")
userName()
passWord()
main()
This was pointed out in the comments above, but if you change
while userInput != username:
and
while passInput != password:
to
while True:
it should work just fine. Then it forces your code to hit the elif statement rather than breaking the loop before printing what you want to say.
In python indentation indicates where a code block starts and begins. So in your while loop in passWord() your if..elif..else must be indented exactly one tab in eg.
while passInput != password:
if len(passInput) == 0:
print("Your password can not be blank, please try again!\n")
passInput = input("Please enter your password.\n")
elif passInput == password:
print("You have successfully logged in!\n")
break
else:
print("Sorry, your password is invalid. Please try again!")
passInput = input("Please enter your password.\n")
Notice how the indentation always goes one tab in for each code block you want to create.

Python 3 quest command, IndentationError: unindent does not match any outer indentation level

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

How can i create a login with a text file

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

Categories

Resources