Python Forum
Need Help With A Problem I Have Come Across
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need Help With A Problem I Have Come Across
#1
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
Reply
#2
you want to use elif not if on line 9
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
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
Reply
#5
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
Reply
#6
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.
Reply
#7
@finndude, please, stop posting unnecessary new threads. Keep the discussion in the original thread.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
(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
Reply
#9
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
Reply
#10
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020