Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
My code refuses to validate the entered username and password even when entered correctly.
Code included below; have I gone wrong anywhere? If so, could you please tell me where?
Cheers.
import time
print ("Before playing, you must register.")
time.sleep(1)
username = open("username.txt","w+")
password = open("password.txt","w+")
print ("Please enter your desired username.")
username_input = input("> ")
print ("Please enter your desired password.")
password_input = input("> ")
username.write(username_input)
password.write(password_input)
username.close()
password.close()
time.sleep(1)
print ("Now, we must authenticate your username and password.")
time.sleep(0.5)
print ("Please input your username.")
u_input = input ('> ')
print ("Please input your password.")
p_input = input ('> ')
username.open("username.txt","r")
password.open("password.txt","r")
u_contents = username.read()
p_contents = password.read()
if u_input == u_contents:
print ("Username authenticated.")
if p_input == p_contents:
print ("Password authenticated.")
else:
print ("Incorrect username or password.")
username.close()
password.close()
Even though you called write(), the contents haven't actually been written yet. File contents aren't written to disk until the file is closed (or flushed) or the program exits.
Close the files after writing them.
For some reason, w+ is not working. I am posting the fully working code which I have changed yours. It is always useful to use with/as with open()import time
import time
print ("Before playing, you must register.")
time.sleep(1)
print ("Please enter your desired username.")
username_input = input("> ")
print ("Please enter your desired password.")
password_input = input("> ")
with open('username.txt', 'w') as u:
u.write(username_input)
u.flush()
u.close()
with open('password.txt', 'w') as p:
p.write(password_input)
p.flush()
p.close()
time.sleep(1)
print ("Now, we must authenticate your username and password.")
time.sleep(0.5)
print ("Please input your username.")
u_input = input ('> ')
print ("Please input your password.")
p_input = input ('> ')
username=''
password=''
with open('xx.txt', 'r') as u:
username = u.read()
u.close()
with open('xxx.txt', 'r') as p:
password = p.read()
p.close()
if u_input == username:
print ("Username authenticated.")
if p_input == password:
print ("Password authenticated.")
else:
print ("Incorrect username or password.")
Output :
C:\Users\Documents>py test.py
Before playing, you must register.
Please enter your desired username.
> mike
Please enter your desired password.
> mike123
Now, we must authenticate your username and password.
Please input your username.
> mike
Please input your password.
> mike123
Username authenticated.
Password authenticated.
Related
I have wrote this simple login and registartion program:
import os
from os import path
def start():
startup = input ('are you an existing user? y/n : ')
if startup == "y":
print ("Lets login then")
login()
if startup == "n":
print ("Lets make you an account")
new_account()
else:
print ("That is an invalid input, please try again")
start()
def new_account():
new_username = input ('what do you want your username to be? ')
print ('okay...')
new_pass = input ('your new password... ')
checker_pass = input ('retype it... ')
if new_pass == checker_pass:
print ('you have entered correct passwords')
print ('Now please login ')
print (' ')
print ('..................................')
print (' ')
saveFile = open( new_username + '.txt','w')
saveFile.write(new_pass)
saveFile.close()
login()
else:
print ('you have done something wrong. Please start again')
new_account()
def login():
print (' ')
print ('..................................')
print ("")
user_name = input ('enter username: ')
file_check = path.exists(user_name + '.txt')
if file_check == False:
print ("That username dosent exist, please start again")
start()
if file_check == True:
Pass_check = open(user_name + '.txt' , 'r').read()
password = input ('enter your password: ')
if Pass_check != password:
print('That didnt quite match. Please start again')
start()
elif Pass_check == password:
print ('Welcome to your account.')
start()
once i have entered a valid username or password it then says:
That is an invalid input, please try again
are you an existing user? y/n :
this is part of the program but isnt supposed to occur once you have been welcomed to your account.
has anyone got a soulution so that once you recieve "welcome to your account" nothing else happens.
i would like it done so that the program dosent fully stop as im looking to put this code into another program.
thanks fin.
THE OUTPUT:
are you an existing user? y/n : y
Lets login then
..................................
enter username: finndude
enter your password: test
Welcome to your account.
That is an invalid input, please try again
are you an existing user? y/n :
i dont want the last two lines to appear
Add a boolean flag value to the start() function that says whether to display the line or not. When calling from login() pass True, when calling recursively from start() then pass False.
The code will only let me guess once . Can someone please tell me what is wrong with my code?
Challenge:
Write a program that sets a password as ‘Gain Access ’ and asks the
user to enter the password and keeps asking until the correct password
is entered and then says ‘Accepted’. The program should count how many
attempts the user has taken and tell them after they have been
accepted.
enter code here
password = 'Gain access'
count = 0
input = input("Enter the password: \n")
while input != password:
print("Incorrect password! Please try again: \n")
count = count + 1
print("You have now got your password wrong " + str(count) + " times. \n")
if(count < 5):
print("Access denied, please contact security to reset your password.")
break
else:
print("Accepted, welcome back.")
print("You had " + str(count) + " attempts until you got your password right.")
You should always include the language you're programming in like simonwo mentioned already.
Looks like Python to me though. I suppose this line input = input("Enter the password: \n") needs to go after while input != password:, as well. Otherwise you can only enter the password once and then it directly executes all 5 loops. But you should NOT assign input because this is the function you want to obtain the input from.
Do something like user_input = input("Enter the password: \n"). So your code should look something like this:
...
user_input = input("Enter the password: \n")
while user_input != password:
print("Incorrect password! Please try again: \n")
user_input = input("Enter the password: \n")
... Your existing code here
But note that this way the user won't get notified if they entered the correct password with their first try. You could insert a check after the first reading of the user input and if it matches the desired password print your welcome phrase.
When I run my program, if I give the right answers to the input, it works perfectly and logs in. But when I give wrong answer, it starts again and when it starts again on a loop, this time even if I give the right answer, it keeps telling me "wrong nickname or password." How do I solve this problem?
kadi = open('kullanici.txt','r')
sif = open('sifre.txt','r')
while True:
ad = input('Your nickname: ')
sifreniz = input('Your password: ')
if not(ad in kadi.read()) or not(sifreniz in sif.read()):
print('Wrong nickname or password.')
continue
else:
print('You succesfully logged in.')
break
you can read the file at once in variable , and then check in variable :
kadi = open('kullanici.txt','r').read()
sif = open('sifre.txt','r').read()
while True:
ad = input('Your nickname: ')
sifreniz = input('Your password: ')
if not(ad in kadi) or not(sifreniz in sif):
print('Wrong nickname or password.')
continue
else:
print('You succesfully logged in.')
break
I'm trying to create a simple username and password script in Python 3.5.1, and I cannot figure out how to make the user input a password, and then for it to be covered up with an asterisk so it cannot be read.
This is my code so far:
import time
print ("Hello!")
time.sleep(1)
print ("Welcome to the Username and Password python test!")
time.sleep(3)
Username = input ("Please enter your desired Username ")
time.sleep(1)
print ("Your username is "+ Username)
time.sleep(1)
Q1 = input ("Is this correct? 'Y' or 'N' ")
if Q1 == "N":
Q2 = input ("Would you like to change it? ")
if Q2 == "Y": Username = input ("Please re enter your Username ")
print ("Your Username is now "+ Username)
time.sleep(2)
print ("You cannot change your Username now")
time.sleep(2)
if Q1 == "Y": time.sleep(1)
Password = input ("Please input your Password ")
time.sleep(3)
Q3 = input ("Please enter your Username ")
if Q3 == Username:
time.sleep(1)
Q4 = input ("Please now input your Password ")
time.sleep(1)
if Q4 == Password:
print ("Correct")
time.sleep(2)
print ("Thank you for using the Username and Password python test")
else:
print ("Incorrect Password")
else:
print ("Username not found")
Use the getpass module for this. getpass.getpass() allows the user to enter input without echoing (printing to the terminal).
You may also want to look at getpass.user() for some of your program's functionality.
I'm trying to make a small program which can tell whether or not the correct password has been entered and keep asking for the correct password until it is entered. The correct password is 'Secret'.
I get as far as creating my while loop. It will ask for the password and ask to enter it again, but it will do this regardless of whether you enter the correct password first time round or not. Where am I going wrong? And how can I get it to break if the correct password is entered first time and how do I keep it asking for the password until it is entered correctly?
This is my code so far:
password = raw_input('What is the password? ')
correctPassword = 'Secret'
tryAgain = raw_input ('Enter password again ')
password
while password == False:
print 'Enter password again '
if password == correctPassword:
print 'You are in!'
break
Try the below code: -
password= raw_input('What is the password? ')
correctPassword= 'Secret'
while password != correctPassword:
password = raw_input ('Enter password again ')
print "Success"
This will execute the while loop until the password input in the while loop is not equal to the correct password.
Here is the right code.
correctPassword= 'Secret'
while True:
password= raw_input('What is the password? ')
if password == correctPassword:
print 'You are in!'
break
print 'Enter password again '
Here is my code
password = 'password'
Get_password = input("Enter the password: ")
while Get_password != password:
print("the password you entered is not correct, enter the correct password")
break
print("You are logged in!")