Python Coursework tidying

126 views Asked by At

I need help with a one or two things on my coursework. firstly when I input an incorrect username/password it will output more than one error message at a time. Secondly, when the men function ends the program will go back to the login loop( that allows the user to re-enter their username/password), I have fixed this by using the B variable but this feels extremely janky and I know there is a better way of doing it. Any Modifications or Tips are greatly appreciated (even if they're not related to my above problems). also the way i code is a bit weird so it's probs best you read from bottom to top :)

def quiz():
print ("quiz")# place holder for real code (let's me know the function has been run)
def report():
print ("report") # place holder for real code (let's me know the function has been run)
def a_menu():# Admin menu
print ("Welcome to the student quiz!")
print ("Please choose a option")
while True:
    print ("1: Computer science")
    print ("2: Histroy")
    print ("3: Reports")
    menu_in = int(input())
    if menu_in == 1:
        Comp_sci = True
        quiz()
        break
    elif menu_in == 2:
        Hist = True
        quiz()
        break
    elif menu_in == 3:
        report()
        break
    else:
        print ("Invalid input! Please try again..")
     def menu():
    print ("Welcome to the student quiz!")
    print ("Please choose a quiz")
    while True:

        print ("1: Computer science")
        print ("2: Histroy")
        menu_in = int(input())
        if menu_in == 1:
            Comp_sci = True
            quiz()
            break
        elif menu_in == 2:
            Hist = True
            quiz()
            break
def login():
b = False
print ("Please enter your username and password")
while True:
    if b == True:
        break
    log_user = input("Username:")
    log_pass = input ("Password:")
    with open("D:\\Computer science\\Computing test\\logindata.txt","r") as log_read:
        num_lines = sum(1 for line in open('D:\\Computer science\\Computing test\\logindata.txt'))
        num_lines = num_lines-1
        for line in log_read:
            log_line = log_read.readline()
            log_splt = log_line.split(",")
            if log_user == log_splt[0] and log_pass == log_splt[2]:
                if log_splt[5] == "a": # Admin will have to mannually change the admin status of other user's through .txt file.
                    b = True
                    a_menu()
                else:
                    b = True
                    log_read.close()
                    menu()
                    break
            else:
                print ("Incorrect username or password, please try again!")
def register():
print ("enter your name, age, and year group and password")
while True:
    reg_name = input("Name:")
    reg_pass = input ("Password:")
    reg_age = input ("age:")
    reg_group = input ("Year Group:")
    print ("Is this infomation correct?")
    print ("Name:",reg_name)
    print ("password:",reg_pass)
    print ("Age:",reg_age)
    print ("Year Group:", reg_group)
    reg_correct = input ("[Y/N]").lower()
    if reg_correct == "y":
        reg_user = reg_name[0:3]+reg_age
        reg_write = open("D:\\Computer science\\Computing test\\logindata.txt","a")
        reg_write.write (reg_user+","+reg_name+","+reg_pass+","+reg_age+","+reg_group+","+"u"+"\n")
        print ("Your username is:",reg_user)
        reg_write.close()
        login()
        break
    elif reg_correct == "n":
        print ("Please Re-enter your infomation")
    else:
        Print ("Invalid input! Please try again...!")
def login_ask():
print ("Do you already have an account?")
while True:
    ask_in = input("[Y/N]").lower()
    if ask_in == "y":
        login()
        break
    elif ask_in == "n":
        register()
        break
  login_ask()
0

There are 0 answers