I would like to enter the word login into the console and have my function run. (I am fairly new to Python so if I don't understand some of the scripts you are sending bear with me.)
I expect the output to be Login successful.
The function I want to run is the following (or Log_in):
def log_in():
print("Username:"); print('Batman_20082')
print("Password:"); print('*******')
print("Logging in! Please Wait")
log_in();
Second Question
Hello!
I am working on a python login logout feature! I want to cancel the logout feature if the login feature hasn't been used.
def log_in():
print("Username:"); print('Batman_20082')
print("Password:"); print('*******')
print("Logging in! Please Wait")
if(input() == 'login'):
log_in()
def log_out():
print("Successfully logged out!")
if(input() == 'logout'):
log_out()
Code Above!
Thanks to GLHR for the help with the if input code!
I would highly appreciate if you help me get to 15 rep! So i can plus rep the people that help me!
I want to be the output to be, you haven't logged in yet. or something like that.
Use the input() method to get user input and compare it to the command login. If they match, run your function:
def log_in():
print("Username:"); print('Batman_20082')
print("Password:"); print('*******')
print("Logging in! Please Wait")
if input() == 'login':
log_in()
input() will wait till the user presses Enter.
If you want to keep asking for the command until a valid command is answered (instead of quitting after the first command), you can use a while loop instead:
while input() != 'login':
print("Invalid command, try again")
log_in()
Related
i wrote a app which takes the inputs from users and if it is matched to what i have selected runs the rest but if it is not say something to user and exit the app but i do not know why this error appear?!
i used the exit at the end of my codes to finish the app completely but this error which shown in pic every time appear
while True:
username=input('please enter your username : \n')
password=int(input('please enter your password : \n'))
if username=='mohammad parsa rezaifar' and password==13801380:
print('congrats :) , you entered')
else:
print('username or password is not matched :(')
exit()
Use break instead of exit()
break exits a for/while loop
Im new to python and I'm trying to code a python login program. Instead of printing out "Welcome to the database" when the username provided is correct, it printed out both "Welcome to the database" and "Username invalid. Please try again.". May I know which part of my code needs to be corrected?
def login():
while True:
name = input("Name: ")
with open('username.txt', "r")as name_file:
for line in name_file.readlines():
if name == line.strip():
print("welcome to database")
else:
print("Username invalid. Please try again")
You are looping through all the users in the text file and for each of them printing to the console. The thing you probably want could be done like this:
def login():
while True:
loginSucessful = False
name = input("Name: ")
with open('username.txt', "r")as name_file:
for line in name_file.readlines():
if name == line.strip():
loginSucessful = True
break
if loginSucessful:
print("welcome to database")
else:
print("Username invalid. Please try again")
You could use a boolean variable to keep track of successful logins like #Michalor did. Or you can use Python's for/else loop to do the same thing without adding a new variable. If the login is successful and you break out of the loop, the "else" statement isn't executed. Using "break" also has the advantage that you don't need to test all of the other users after you have found a successful login.
def login():
while True:
name = input("Name: ")
with open('username.txt', "r")as name_file:
for line in name_file.readlines():
if name == line.strip():
print("welcome to database")
break
else:
print("Username invalid. Please try again")
Of course, this kind of function doesn't provide much security, as you can keep guessing the names in the text file until you find a valid one, or if you can get your hands on the text file itself you can just look the names up. For actual login code, it's probably best to use some kind of login library that handles the security details for you.
so I am trying to create a program that will allow a user to login using a username and password for a school project. however, my teacher (he has allowed me to ask FYI) wants us to think of a way to make it secure.
so my thought process is that I would allow the user to create a login and store the usernames and passwords in a notepad file. to make these secure I decided to use the hash() function so that the username and passwords couldn't be seen even if the text file was accessed. the issue that I am running into is that I can't figure out how to get the program to see the saved hash version of the username and password in the text file and then compare it to the inputs for longing in without printing the hashes and or saving them as variables.
I can't do this however because if I have more than one login saved in the login file I cant save all the hashed logins as one variable.
if anyone can help it would be greatly appreciated
import sys
import time
print ("welcome to this quiz.")
account = input ("first off do you have a account already. please enter yes or no only").lower
if account == no:
account_create = input ("to continue with this quiz you need a password would you like to create a account. please enter yes or no only").lower
if account_create == no:
print (" the program will close in 30 seconds. if you change your mind come back and make an account")
time.sleep(30)
sys.exit
else:
print ("thank you for creating an account")
username = input ("first off you need a username. please enter a username. be carefull once it is chasen it cant be changed")
# need to add a function that searches the login file and compares no username is repeated
password = input ("secondly you need a password. please choose a password. be carefull you can change it later but you will need the current one to do this.")
username = hash(username)
password = hash(password)
file = open("Login.txt","w")
file.write (username)
file.write (",")
file.write (password)
file.write("\n")
file.close()
print ("Your login details have been saved. ")
print ("now please login")
else:
login? = input ("would you like to login to the program").lower
if login? == no:
print ("please come back another time")
time.sleep(20)
sys.exit
else:
username_check = input ("please enter your username")
password_check = input ("please enter your username")
username_check = hash(username_check)
password_check = hash(password_check)
file = open("Login.txt","r")
if username_check ==
Load the file as a dictionary mapping usernames (or their hashes) to the hash of the password.
You can iterate through the file and load each line into your dictionary.
However this will be a bit fragile. What if some user want to have a space or comma in their username. A better and easier approach is to use a library to serialize and deserialize the dictionary. The common one used in python would be pickle or if you want the data to still be somewhat human readable json.
I have did a little research around "Google", "YouTube", "Facebook" and "Stack Overflow" and I haven't found what I was looking for. So, I need your guidance. :)
I want program to ask user input "PASSWORD" and every time user inputs wrong password the program asks password again, and again, and again until user
types the correct password. Let's say that the password is as simple as "abc123".
So, I start the program and it asks to input: "PASSWORD: ". If user types "abc123" then program prints "GOOD PASSWORD". If user types anything what is not "abc123" then program prints "BAD PASSWORD". As simple as that.. for now.
My best attempt:
#SECTION1_ASKING
passwordInput=input('PASSWORD: ')
password='abc123'
while password == passwordInput:
print('GOOD PASSWORD')
break
else:
print('BAD PASSWORD')
passwordInput=input('PASSWORD: ')
#SECTION2_RE-ASKING
while False:
while password == paswordInput:
print('GOOD PASSWORD')
else:
print('BAD PASSWORD')
passwordInput=input('PASSWORD: ')
but I either make password asked once or twice or I stuck in Infinite while Loop.
Try this:
passwordInput=raw_input('PASSWORD: ')
password='abc123'
while True:
if password == passwordInput:
print('GOOD PASSWORD')
break
else:
print('BAD PASSWORD')
passwordInput=raw_input('PASSWORD: ')
You can do as below in few lines.
password='abc123'
while(True):
passwordInput=input('PASSWORD: ')
if(passwordInput==password):
print("Good password")
break
else:
print("Bad password")
Here is my solution:
password = 'abc123'
while True:
passwordInput = input('PASSWORD: ')
if password == passwordInput:
print('GOOD PASSWORD')
break
else:
print('BAD PASSWORD')
How does that differ from yours? For a start you can see that I only have one call to input(), that's generally a good idea because then you only need to check the password in one place. Notice that I use if instead of while for the test.
In your second example you have while False:. Can this ever be True? No, so it won't get executed.
Notice as well that I use more whitespace. That does not slow the program down, it makes it easier to read. See also PEP008
Now you have it working, just for fun, consider an improvement. Normally you don't want the password to be visible when it is typed, but there's a module for that: getpass - it's part of the standard library so you don't need to download anything.
Add import getpass to the top of your script. Instead of input use getpass.getpass, in the same place in the program, with the same parameters. Now it won't echo the password entered by the user.
getpass is the module name, getpass is also a function name, so getpass.getpass('PASSWORD: ') means execute the getpass() function in the getpass module. We use lots of modules in Python, so its worth getting used to using them. You can find the documentation here, note there is also a getpass.getuser() to play with.
This question already has answers here:
Getting a hidden password input
(6 answers)
Closed 7 months ago.
I have been trying to make a secure login on a python program I've been working on, but everything I try doesn't seem to help. I need a snippet of code that I can put in my script. My main issue is that anyone who looks at my script can just see the password. Plus I don't know how to put stars instead of characters when they type in password. This is my login code, it is very basic because I'm pretty much a beginner.
#login
import webbrowser as w
def username():
print("Enter UserName")
usrnm = input()
if(usrnm == "example"):
password()
else:
notusername()
def notusername():
print("Try Again")
username()
def password():
print("Enter Password")
pswrd = input()
if(pswrd == "password"):
w.open("www.example.net")
else:
notusername()
username()
First, let me preface this by saying that, especially if you're a beginner, it is usually not a good idea to try to implement your own login/security code for anything that is public and seriously needs security.
Having said that, the general approach to hiding the actual password is to store a hash (e.g. SHA-1) of the password, not the password itself. You can then safely store that hash value wherever you like (e.g. database, text file etc.)
In python you can do this using something like hashlib e.g.
import hashlib
sh = hashlib.sha1()
sh.update('password')
hash_value = sh.hexdigest()
# write hash_value to file/db...
When you go to validate against the stored password, you take the hash of the user input and compare it against the stored hash. If they are the same, then the password is correct.
Again, for any serious security, use one of the many frameworks that are available, as they have been tested by many people.
You Should try this code
list1 = ('Password is Correct...' , 'Password is Incorrect' , 'Closing Python...','Hello',
'''Press Enter to Continue...''', 'Closing Python...' , 'badger123',
'''Please Enter Your Name: ''', 'Please Enter Your Password: ')
name = input(list1[7])
password = input(list1[8])
if password == list1[6]:
print(list1[0])
else:
print(list1[1])
exit()
import time
time.sleep(0)
input(list1[4])
time.sleep(0)
print (list1[3] , name)
import time
time.sleep(1)
print (list1[5])
import time
time.sleep(5)
input (list1[4])
exit()