Python (LoginSystem) Überprüfung ob Nutzername schon existiert

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
Ducky.py
User
Beiträge: 1
Registriert: Samstag 17. April 2021, 15:04

Bin gerade dabei ein LoginSystem mit Python zu bauen. Hab ein Problem was Ich selbst durch Googlen nicht beheben konnte in der Funktion Def Register(): muss überprüft werden ob der Nutzername schon existiert, wenn ja gibt er UserName already exist! aus wenn nicht dann Username ok! trotzdem bleibt er in der For Schleife und kommt zu keinem Ende also Endlos.

Code: Alles auswählen

import random
import re
#Guest Number
number = random.randint(1023,9999)#The Random Numbers for Guest####

def Register():
    with open('DataList/DataList.txt') as f: #opens the DataList folder and the Txt file it contains
        data = f.read() #reads the Txt file
        for user in data: 
            UserName = input("Enter your Username: ")
            if UserName in data:#checks if the username is in Data 
                print("UserName already exist!")# If yes the output will be print
                continue
            elif not UserName in data:#If the username is not in the txt file
                print("Username ok!")#this is output
                continue
            elif len(UserName) <= 3:#must not be smaller than 3 
                print("Username is to Short")
                continue
            elif len(UserName) >= 18:#must not be greater than 18
                print("Username is to long")
            # while not UserName:#If Input empty output this 
            #     UserName = input("Please input your Username: ")




#Program query 
while True:
    UserInput = input("Hello Guest#{numbers} Do you have a Account y/n: ".format(numbers=number))#Query whether an account exists or not 
    if UserInput == "n":#If not give `n` a
        Register()
    # elif UserInput == "y":#If yes then enter `y`
    #     Login()
    else:
        print("None Valid form!")#If the input does not apply at all or is given 
    
Sirius3
User
Beiträge: 17749
Registriert: Sonntag 21. Oktober 2012, 17:20

Variablennamen wie auch Funktionen schreibt man nach Konvention komplett klein.
Was soll die for-Schleife für einen Sinn haben, wenn Du `user` gar nicht verwendest?
Ein einfache in-Prüfung auf den Inhalt der Datei ist etwas zu grob, denn "Duck" ist im Namen "Ducky" auch enthalten.
Wenn Du ein ›UserName in data‹ und ein ›not UserName in data‹ dann ist zweiteres das exakte Gegenteil von der ersten Bedingung und sollte als ›else:‹ geschrieben werden. Alle weiteren elif-Bedingungen werden nie erreicht.
Antworten