I can't seem to repeat this process [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 days ago.
Improve this question
system_message = 'Password confirmation successful'
print("Welcome to password confirmation.")
password = input('Please enter your password. ')
re_entered_password = input('Please re-enter your password ')
if re_entered_password == password:
print(system_message)
else:
system_message = 'Password confirmation unsuccessful'
print(system_message)
while system_message == 'Password confirmation unsuccessful':
system_message = 'Password confirmation successful'
print("Welcome to password confirmation.")
password = input('Please enter your password. ')
re_entered_password = input('Please re-enter your password ')
if re_entered_password == password:
print(system_message)
else:
system_message = 'Password confirmation unsuccessful'
print(system_message)
Basically I'm trying to loop it so that after the program is successful it'll still be able to continue the process.
I tried the continue code but it didn't seem to work.

system_message = 'Password confirmation unsuccessful'
print("Welcome to password confirmation.")
while system_message == 'Password confirmation unsuccessful':
password = input('Please enter your password. ')
re_entered_password = input('Please re-enter your password ')
if re_entered_password == password:
system_message = 'Password confirmation successful'
print(system_message)
else:
print(system_message)

Related

Why is my program not looping even if the counter works? [closed]

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 6 months ago.
Improve this question
Why is my program not looping back to the 3rd line whenever I have exhausted all my attempts?
counter = 1;
for i in range(1):
password = input('Enter your desired password: ');
print('Registration successful');
x = 5;
for i in range(5):
print(' Login ');
login = input('Enter password: ');
if login == password:
counter -= 1
print(' Welcome ');
print('Account Information: ');
print('Name: ',name);
print('Age: ',age)
break;
else:
x=x-1;
if x==0:
print('Account locked');
else:
print(' Wrong password');
print(x,' trial(s) left ');
You can simplify and start to make the code reusable by putting it into a function. Also, it's a good idea to make it pythonic and remove all the ; that will only serve to confuse the reader as to what language they're looking at.
def login(name, age):
password = input('Enter your desired password: ')
print('Registration successful')
tries = 5
for i in range(tries):
print(' Login ')
login = input('Enter password: ')
if login == password:
return True
else:
print(' Wrong password')
print(f'{tries-1-i} trial(s) left ')
return False
name = 'Bob'
age = 12
if login(name, age):
print(' Welcome ',
'Account Information: ',
f'Name: {name}',
f'Age: {age}',
sep='\n')
else:
print('Account locked')
Example Output:
# Success 1st try:
Enter your desired password: password
Registration successful
Login
Enter password: password
Welcome
Account Information:
Name: Bob
Age: 12
# Success 3rd try:
Enter your desired password: password
Registration successful
Login
Enter password: pass
Wrong password
4 trial(s) left
Login
Enter password: pass
Wrong password
3 trial(s) left
Login
Enter password: password
Welcome
Account Information:
Name: Bob
Age: 12
# Fail:
Enter your desired password: password
Registration successful
Login
Enter password: pass
Wrong password
4 trial(s) left
Login
Enter password: pass
Wrong password
3 trial(s) left
Login
Enter password: pass
Wrong password
2 trial(s) left
Login
Enter password: word
Wrong password
1 trial(s) left
Login
Enter password: pasword
Wrong password
0 trial(s) left
Account locked
I got this working.
def login():
counter = 1;
x = 5;
for i in range(5):
print(' Login ');
login = input('Enter password: ');
if login == password:
counter -= 1
print(' Welcome ');
print('Account Information: ');
print('Name: ',name);
print('Age: ',age)
break;
else:
x=x-1;
if x==0:
print('Account locked');
start();
else:
print(' Wrong password');
print(x,' trial(s) left ');
def start():
for i in range(1):
global password
password = input('Enter your desired password: ');
print('Registration successful');
login();
start();

How do I correctly format putting an "if" in python

How would I put "if" or "else" in code like this?
user = input('Login: Username\n')
time.sleep(1)
password = input('Login: Password\n')
print('Welcome, %s.' % user)
I think this is what you want to do. But this is not a secure way of doing password requests.
user = input('Login: Username\n')
password = input('Login: Password\n')
users = {"You" : "1234", "Me" : "abcd"}
if user in users.keys():
if users[user] == password:
print("Welcome, {u}".format(u=user))
else:
print("Wrong password")
else:
print("This user does not exist")
Here is a simple version on how this should work but is not secure at all, but functions (almost) the same way as you requested (came up with this under 5 mins)
user = input('Login Username: ')
password = input('Login Password: ')
if user == ("Your username here"):
if password == ("Your password here"):
print("Welcome ", user)
Also you do not need "time.sleep()" as it only slows down your code

Python Password Refuses to be Validated [closed]

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.

python read/write to file login script

I was wondering if anyone would help. I am new to python. I am trying to create a basic login script for a game, that will write a username and password to a text file. When logging in, it will read from that text file and compare the entry made by the user. The code is below:
def Register():
print("Hello! You need to register an account before you can begin")
username = input("Please enter a username: ")
password = input("Now please enter a password: ")
file = open("Login.txt","a")
file.write (username)
file.write (",")
file.write (password)
file.write("\n")
file.close()
print ("Your login details have been saved. ")
print("You will now need to login")
Login()
def Login():
print("Please enter your details to log in")
username1 = input("Please enter your username: ")
password1 = input("Please enter your password: ")
file = open("Login.txt","r")
for row in file:
field = row.split(",")
username = field[0]
password = field[1]
lastchar = len(password)-1
password = password[0:lastchar]
print(username,password)
if username1 == username and password1 == password:
print("Hello",username)
else:
print("incorrect")
#file.close()
user=input("Are you already a user? ")
if user == "Yes":
Login()
elif user =="No":
Register()
print("Welcome to our game")
I have entered the second user who is stored in the text file, It seems to be working but it checks my first entry and says its incorrect and then loops to the second entry. This is the output I keep getting:
Are you already a user? Yes
Please enter your details to log in
Please enter your username: jen
Please enter your password: ben
tess bess
incorrect
jen ben
Hello jen
Welcome to the dice game
>>>
Does anyone have an idea on how to only display the entry you have entered?
Thanks
Like Sharku said, put the print(username,password) in your if below. Also writting clearly the name and password of the user after he typed it isn"t really a smart moove, delete it and just let your message when a user is logging in !
for row in file:
field = row.split(",")
username = field[0]
password = field[1]
lastchar = len(password)-1
password = password[0:lastchar]
if username1 == username and password1 == password:
print("Hello",username)
else:
print("incorrect")
As said by sytech, you could use the break and else clauses of the for loop:
for row in file:
field = row.split(",")
username = field[0]
password = field[1]
lastchar = len(password)-1
password = password[0:lastchar]
if username1 == username and password1 == password:
print("Hello",username)
break
else:
print("incorrect")
Your 'for loop' will loop through each entry in your file, that means for each entry in the file your for loop prints the entry due to this line:
print(username,password)
If you don't want it to print all values in the file remove this line of code.
Adding a 'break' to your if statement, as suggested by others, will mean that as soon as your loop has found the entry that matches the one entered by the user it will leave the loop and not continue going through all values unnecessarily.
You could do something like this:
if username1 == username and password1 == password:
print("Hello",username)
break
else:
continue
This means that when a user input doesn't match an entry in the file the loop will just continue till it finds a match.
However, your code doesn't take into consideration if a user doesn't exist.
import os.path
if not os.path.exists('register.txt'):
file = open('register.txt', 'w')
file.close()
def register():
username = input('Enter username: ')
if username in open('register.txt', 'r').read():
print('Username already exists')
exit()
password = input('Enter password: ')
c_password = input('Enter confirm password: ')
if password != c_password:
print('Sorry password not match')
exit()
handle = open('register.txt', 'a')
handle.write(username)
handle.write(' ')
handle.write(password)
handle.write('\n')
handle.close()
print('User was successfully registered')
exit()
def login():
username = input('Enter username: ')
password = input('Enter password: ')
get_data = open('register.txt', 'r').readlines()
users_data = []
for user in get_data:
users_data.append(user.split())
total_user = len(users_data)
increment = 0
login_success = 0
while increment < total_user:
usernames = users_data[increment][0]
passwords = users_data[increment][1]
if username == usernames and password == passwords:
login_success = 1
increment += 1
if login_success == 1:
print('Welcome ' + username)
else:
print('invalid username & password')
question = input('Do you have an account?/yes/no')
if question == 'yes':
login()
else:
register()

Getting while loops to continue in Python

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

Categories

Resources