Posts: 73
Threads: 28
Joined: Mar 2020
Mar-24-2020, 04:15 PM
(This post was last modified: Mar-24-2020, 04:18 PM by buran.)
Hello Users.
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:
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
Posts: 8,090
Threads: 154
Joined: Sep 2016
you want to use elif not if on line 9
Posts: 8,090
Threads: 154
Joined: Sep 2016
Mar-24-2020, 04:29 PM
(This post was last modified: Mar-24-2020, 04:29 PM by buran.)
Cross-posted at SO: https://stackoverflow.com/q/60835098/4046632
Please, don't cross-post or at least provide a link. Otherwise it's a waste of time for people helping you.
Posts: 73
Threads: 28
Joined: Mar 2020
Hello Users,
ive coded a simple login and registration 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()
elif 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:
enter your password:
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:
Output: are you an existing user? y/n : y
Lets login then
..................................
enter username: finndude
enter your password: test
Welcome to your account.
enter your password:
i dont want the final line of this
thanks, fin
Posts: 73
Threads: 28
Joined: Mar 2020
Hello Users,
ive coded a simple login and registration 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()
elif 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:
enter your password:
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.
Output: are you an existing user? y/n : y
Lets login then
..................................
enter username: finndude
enter your password: test
Welcome to your account.
enter your password:
i dont want the final line of this
thanks, fin
Posts: 444
Threads: 1
Joined: Sep 2018
The only thing that comes to mind is the recursion on line 64. Is there anything in addition the the output snippet you provided? I can see this happening if you answer "y", entered a non-existent username, created an account, and then logged in. Otherwise, I do not see how this could occur.
On a related note, it would be better to implement these features with loops instead of recursion. There is the possibility of someone trying to brute force it and causing a stack overflow.
Posts: 8,090
Threads: 154
Joined: Sep 2016
@ finndude, please, stop posting unnecessary new threads. Keep the discussion in the original thread.
Posts: 73
Threads: 28
Joined: Mar 2020
(Mar-24-2020, 10:33 PM)stullis Wrote: The only thing that comes to mind is the recursion on line 64. Is there anything in addition the the output snippet you provided? I can see this happening if you answer "y", entered a non-existent username, created an account, and then logged in. Otherwise, I do not see how this could occur.
On a related note, it would be better to implement these features with loops instead of recursion. There is the possibility of someone trying to brute force it and causing a stack overflow.
Hi,
On line 64 there is no code so what would u say needs to be changed there?
Yes that was the only output that occurred.
If needs be you could copy the code and put in your edits to make it clearer.
Thanks for the help so far
Fin
Posts: 73
Threads: 28
Joined: Mar 2020
Mar-27-2020, 03:46 PM
(This post was last modified: Mar-27-2020, 04:03 PM by buran.)
Hello Users, i have come across this problem with this code. i am trying to save the values to a file but get this error i have never come across before:
amount_of_player_1_cards = str(len(player_1_cards))
amount_of_player_2_cards = str(len(player_2_cards))
player_1_results = str(username_1) + " : " + str(amount_of_player_1_cards)
player_1_results = str(username_2) + " : " + str(amount_of_player_2_cards)
######################################################################################################
save_score = open("Card Game Scores" + ".txt","w").write() #looking at what is in the file
save_score.write(player_1_results) #writes in the file what the users password is
save_score.write(player_2_results) #writes in the file what the users password is
save_score.close() #saves and closes the file the output i receive is:
Error: Traceback (most recent call last):
File "E:\Python\Code\Finley Wallace - Wright - Controlled Assesment - Task 3.py", line 2693, in <module>
save_score = open("Card Game Scores" + ".txt","w").write() #looking at what is in the file
TypeError: write() takes exactly one argument (0 given)
if anyone can help it would be much appreciated
thanks,
fin
Posts: 8,090
Threads: 154
Joined: Sep 2016
Mar-27-2020, 04:05 PM
(This post was last modified: Mar-27-2020, 04:07 PM by buran.)
save_score = open("Card Game Scores.txt","w").write()
you don't need .write() here. What it is supposed to do anyway?
Also, better use a with context manager:
with open("Card Game Scores.txt","w") as save_score: #looking at what is in the file
save_score.write(player_1_results) #writes in the file what the users password is
save_score.write(player_2_results) #writes in the file what the users password is note that you don't write new line, so I guess the output won't be exactly what you expect.
|