0
votes

I programmed a python file that allows you to register (Name, Password, EMail (with Confirmation EMail sent to you with a code)) and after you are registered your data is being saved in a seperate text file now I want python to check when you enter your username whether the username already exists or not.

There is this names.txt file and it is just 1Line: Username 2Line: Password 3Line: email adress when you register a new enter code here unt it will show the same below the first account in the txt file. But I want python to check this text file now during your registration whether you can use this username because its not used so far, or if the username already exists and you have to take another one.

import smtplib
import random
random.seed()

registrated=False

print("")
print("Namen Kriterien:")
print("-Mindestens 1 Buchstabe")
print("-Mindestens 4 Zeichen lang, maximal 20!")
print("-'Thats it!'")
print("")

fehler=1
while fehler==1:
    try:
        print("")
        name=input("Geben Sie Ihren Nutzernamen ein (4-20 Zeichen): ")
        if len(name) > 20 or len(name) < 4:
            print("Dein Name entspricht nicht den Kriterien!")
        else:
            print("")
            print("Dein Name ist:",name)
            print("")
            fehler2=1
            while fehler2==1:
                try:
                    q1=input("Ist das richtig? (YES | NO): ")
                    q1.upper()
                    if q1=="YES":
                        print("")
                        fehler2=0
                        fehler=0
                    elif q1=="NO":
                        print("")
                        fehler2=0


                    else:
                        print("")
                        print("ERROR")
                        print("")
                        fehler2=1
                except:
                    print("")
                    print("ERROR")
                    print("")
                    fehler2=1

    except:
        print("Versuche es nochmal!")
        fehler=1

print("Herzlichen Glückwunsch, dein Name ist",name)
fehler1=1
while fehler1==1:
    try:
        print("")
        pw=input("Bitte gib dein Passwort ein (selbe Kriterien wie der Name): ")
        if len(pw) > 20 or len(name) < 4:
            print("Das Passwort entsprcht nicht den Kriterien!")
        else:
            pw1=input("Bitte bestätige dein Passwort: ")
            if pw1==pw:
                fehler1=0
            else:
                print("")
                print("DEIN PASSWORT WAR FALSCH, BITTE WIEDERHOLEN!")
                print("")
                fehler1=1
    except:
        print("Versuche es nochmal!")
        fehler1=1
#Mail
import smtplib
import random
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
random.seed()

while 1:
    print("")
    email=input("Please enter your email adress: ")
    email1=input("Please confirm your email adress: ")
    if email == email1:
        break
    else:
        print("Something went wrongt, try again!")

email_user='[email protected]'
email_pw='mnstudiophytonnht1102'
code=random.randint(10000,99999)
body='Hi there, thanks for using mnstudio programs, your code is: '+str(code)
subject='Registration to Mnstudio!'
msg = MIMEMultipart()
msg['From'] = email_user
msg['To'] = email
msg['subject'] = subject
msg.attach(MIMEText(body,'plain'))
text = msg.as_string()


server= smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login(email_user,email_pw)

fh=1
fhh=1
while fh==1:
    try:
        server.sendmail(email_user,email,text)
        print("")
        print("DONE EMAIL SENT")
        print("")
        print("We sent you an email with the confirmation code, please check spam as well if you cant find it!")
        print("")
        fh=0
    except:
        fhh=1
        print("")
        print("SOMETHING WENT WRONG WE'LL TRY AGAIN")
        print("")
        while fhh==1:
            try:
                print(email)
                q=input("Is the email correct? (YES | NO): ")
                print("")
                q.upper()
                if q=="YES":
                    fhh=0
                    fh=1
                elif q=="NO":
                    fhh=1
                    email=input("Please enter your email again: ")
                    email1=input("Please confirm email: ")
                    if email==email1:
                        print("")
                        print("Thanks we'll try to send an email to,",email)
                        print("")
                        fhh=0
                        fh=1
                    else:
                        print("ERROR TRY AGAIN")
                        fhh=1
                        fh=1
                else:
                    fhh=1
                    print("ANSWER WITH (YES | NO)")
            except:
                print("ANSWER WITH (YES | NO)")
        fh=1

server.quit()


while True:
    codeconfirm=int(input("Bitte geben Sie den Code ein: "))
    print("")
    if codeconfirm==code:
        registrated=True
        print("CODE CORRECT")
        print("")
        break
    else:
        print("Code war falsch bitte noch einmal eingeben !")


#
if registrated:
    dateihandler = open('names.txt', mode='a')

    dateihandler.write("\n"+str(name))
    dateihandler.write("\n"+str(pw))
    dateihandler.write("\n"+str(email))
    dateihandler.write("\n") 

print("Danke du bist jetzt registriert! ")
input("")
1
You should be using a database, not a text file.cs95
Upload your code so that we can see what is working and what is not.Tatsuya Yokota
@cᴏʟᴅsᴘᴇᴇᴅ is right, you should use a database and use some technique as index or primary key. It will improve your project.Juan Antonio
Ok how can I connect a database?Markus Nesshyba
Code is uploadedMarkus Nesshyba

1 Answers

0
votes

If you load the file and load usernames in a simple list you can check if new username exists or not in the list with simple is ... in ... but is not very clean solution, but if you are doing some exercise or something like this is a good first step, but I repeat, I think that your solution to the problem that I imagine that you have is not good in a real scenario.