Ending the program without the rest - need support - python

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.

Related

i am having trouble with open() command in python

I am currently developing a desktop login-register app for my practice, and was having some trouble with login and register. If I type REGISTER and add my info in f.write() command it stores my info but after that everything just goes, like the whole file gets formatted (this was register issue).
The login issue is if I want to check whether a name or password in file exists or not (this command could be wrong). I tried to use if login_email and login password in f: but it says that login_email and password do not exist.
Code:
f = open('pass.txt', 'w')
fr = open('pass.txt', 'r')
from time import sleep
login_list = "LOGIN"
register_list = "REGISTER"
if 1 > -3232:
print("Type register for new account\ntype login for login into existing account")
bi = input("==> ")
if bi.upper() in login_list:
print("you are registered?? nice now loginnn!!")
login_1 = input("your username: ")
login_2 = input("your password: ")
if login_1 and login_2 in fr:
print("Nice my program worked??")
exit()
else:
exit()
elif bi.upper() in register_list:
print("you are in register section: ")
sleep(.9)
print("NOTE: Your password should only contain alphabets!")
sleep(4)
reg_1 = input("your username: ")
sleep(.9)
reg_2 = input("your password: ")
sleep(.9)
reg_2v1 = input("confirm password")
if reg_2 == reg_2v1:
f.write(reg_1 + " : " + reg_2 + "\n")
print("now login again,\")
else:
print("invalid password, try again")
else:
print("you gave me the wrong command")
else:
exit()
You shouldn't open the file in both read and write mode at the beginning of the script. Opening it in write mode empties the file, so you won't be able to read it. You'll also wipe out all the other usernames and passwords. You should open the file in read mode when logging in, and append mode when registering, to add a new line without removing the old ones. And you should use with to just open the file around the code that needs to use it.
if login_1 and login_2 in fr: is not the correct way to test if both the username and password are in the file. Due to operator precedence, that's parsed as if login_1 and (login_2 in fr):. This just checks that login_1 is not empty, and then just checks if login_2 is in the file. The second test will never work, because the lines of the file all end with newline, but login_2 doesn't, so they'll never match.
You need to check for the fully formatted line, including the newline.
if f'{login_1} : {login_2}\n' in fr:
if bi.upper() in login_list: seems suspicious. login_list is not a list, it's a string. So this will check whether bi.upper() is any substring -- it will succeed if the user enters log or in or gi, not just login. Is that intentional?
Full code:
from time import sleep
login_list = "LOGIN"
register_list = "REGISTER"
if 1 > -3232:
print("Type register for new account\ntype login for login into existing account")
bi = input("==> ")
if bi.upper() in login_list:
print("you are registered?? nice now loginnn!!")
login_1 = input("your username: ")
login_2 = input("your password: ")
with open('pass.txt', 'r') as fr:
if f'{login_1} : {login_2}\n' in fr:
print("Nice my program worked??")
exit()
else:
exit()
elif bi.upper() in register_list:
print("you are in register section: ")
sleep(.9)
print("NOTE: Your password should only contain alphabets!")
sleep(4)
reg_1 = input("your username: ")
sleep(.9)
reg_2 = input("your password: ")
sleep(.9)
reg_2v1 = input("confirm password")
if reg_2 == reg_2v1:
with open('pass.txt', 'a') as f:
f.write(reg_1 + " : " + reg_2 + "\n")
print("now login again,")
else:
print("invalid password, try again")
else:
print("you gave me the wrong command")
else:
exit()

Why does my sub-routine continue to carry out all of my if statements after I've called another sub-routine

My logins() sub routine will continue to carry out both the else and elif parts after it has found a login and has verified the password. I cant seem to understand why its doing this but is really halting my progress.
enter image description here
def login ():
Username_Input = input("Please enter your username : ")
logins = open("logins.csv","r")
List_Information = list(csv.reader(logins))
for x in List_Information:# loops through all lists
if x[0] != Username_Input :
print("Username not found please register ")
register ()
else:
Password_Input = input("Username found please enter your password : ")
for x in List_Information:
if x[1] == Password_Input :
print("Loged in lets get this game going. ")
game()
else :
print("nope sorry password not found lets go back to the menu : ")
Menu()
After it has found the correct password it will continue going through the List_information after calling game(). The call to game() would not stop the looping and thus it finds the next user from the List_information and say that the password was wrong.
You should be finding the correct entry from the List_information (based on the username) and the check the password against that entry. Now you are basically only comparing the first element in the List_information.
Something like this:
user = None
for x in List_information:
if x[0] == Username_input:
user = x
if user == None:
print("Username not found please register ")
register ()
else:
Password_Input = input("Username found please enter your password : ")
if user[1] == Password_input:
game()
else:
Menu()

Why is my password programme code not working?

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.

code that i've written works perfectly when in "if" statement but doesn't work in "else" statements

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

Loging and saving to csv

I'm making login/signing section for my code. I have 2 issues with it. I need help:
First question Yes or No functions well until other character entered. While loop is not accepted for some reason. How to get back to beginning until Y or N entered?
I would like to store dict with usernames and passwords as CSV file sorted in two columns not rows. How to do it.
Thanks
Here is the code....
# CREATING START DICTIONARY
users = {"guest": "guestpass", "admin": "adpass"}
status = input("\rAre you a registered user? Y / N? ").upper()
while status != "Y" and status != "N":
print ("Please enter Y or N")
# NEW USER
if status == "N":
createLogin = input("Create login name: ")
if createLogin in users: # check if login name exist in the dictionary
print("Login name already exist! Please, choose another one.\n")
else:
createPass = input("Create password: ")
retypePass = input("Retype password: ")
while True:
if createPass != retypePass:
print("\nPassword error!\n")
else:
users.update({createLogin : createPass})
print("\nNew user created! Welcome to ARR!\n")
break
import csv
writer = csv.writer(open('UsersFile.csv', 'wb'))
for key, value in users.items():
writer.writerow([createLogin, createPass])
# LOGIN EXISTING/NEW USER
elif status == "Y":
while True:
loginName = input("Enter login name: ").lower()
if loginName not in users:
print("User doesn't exist! Please enter existing name or sign-in.")
print("----------------------------------------------------------")
else:
passw = input("Enter password: ")
# LOGIN MATCHES PASSWORD
if loginName in users and passw != users.get(loginName):
print("Wrong password! Please enter username and password again!")
else:
print("Login successful!\n")
break
1) In your y/n while loop you are missing a tab to indent the print statement.
2) https://docs.python.org/2/library/csv.html
Some issues I see so far:
Indentation. print ("Please enter Y or N") should be indented relative to the while statement on the previous line.
Also, the statement if createLogin in users and the following else statement should probably be indented one more level, if they are meant to be within the if status == 'N' statement.
Import statement. Generally, things like import csv would be at the top of the file. Try moving it there and see if it helps.

Categories

Resources