My chatapp isnt working :(

Question:

I am trying to make a chatapp but when a user is done signing up or logging in it doesnt kick the user to the menu portal/page.
I think its an issue with the if and elif statements.
maybe its the guest, admin and creator vars?

I tried to add some elif statements and some returns in the login, guest, and signup functions and that didnt work either.
I expected that after a user logins, signs up, or becomes guest it would kick the user to the menu page.
Also, the guest if statement is only working…
Heres the code:

def signup(username, password, dis_name, lvl, default_img="https://www.kindpng.com/picc/m/451-4517876_default-profile-hd-png-download.png"):
    spec = f"/mnt/student/MuskHub/data/base/{username}nardx.json"
    
    # Check if the user already exists
    if os.path.exists(spec):
        print("Account already exists...\nTry logging in again :)")
        return
    banned = [# some banned names]
    #if username in banned:
        #print("Unkown error!")
    # Prepare the user data as a dictionary
    # Adjust the display name based on user level
    if lvl == "creator":
        dis_name = f"{dis_name} \033[1;32m(MuskHub Founder and Mod)\033[0m"
    elif lvl == "admin":
        dis_name = f"{dis_name} \033[1;31m(MuskHub Admin 🛡)\033[0m"
    
    # Construct the user data dictionary
    user_data = {
        "username": username,
        "password": password,
        "display_name": dis_name,
        "avatar": default_img,
        "level": lvl,
        "bio": "not given",
        "location": "not given",
        "status": "not given",
        "banned": False,
        "impersonating": False,
    }
    pec = "/mnt/student/MuskHub/data/base/usersnardx.txt"
    userstxt = "/mnt/student/MuskHub/data/base/usersnardx.txt"
    # Write the user data to a JSON file
    try:
        with open(spec, 'w') as file:
            json.dump(user_data, file, indent=4)  # Use indent for pretty printing
        with open(pec, 'a') as u:
            u.write(f"\n{dis_name}")
        with open(userstxt, 'a') as ty:
            ty.write(f"\n{username}")
        print("Signed up! To edit account settings please sign in and type n1 in menu.")
        return
    except Exception as e:
        print("MuskHub encountered an error.\nPlease contact Praise-Him ASAP")
        return
def login(username, password):
    pec = f"/mnt/student/MuskHub/data/base/{username}nardx.json"
    logintxt = "/mnt/student/MuskHub/data/lognardx.txt"
    
    if not os.path.exists(pec):
        print("Account doesn't exist...\nPlease sign up to create an account.")
        return    
    with open(pec, 'r') as peck:
        user_data = json.load(peck)
        if user_data['password'] == password:
            print("Logged in.")
            dis_name = user_data['display_name']
            x = datetime.now()
            user_type = 'user'
            if 'lvl' in user_data and (user_data['lvl'] == "admin" or user_data['lvl'] == "creator"):
                user_type = "admin"
                admin = True
            time = x.strftime("%I:%M %p on %B %d, %Y  (UTC format)")
            with open(logintxt, 'a') as pi:
                pi.write(f"\n{dis_name} logged in at {time}\n")
            return
        else:
            print("Your password is incorrect!")
            return 
def guester(name):
    logintxt = "/mnt/student/MuskHub/data/lognardx.txt"
    x = datetime.now()   
    time = x.strftime("%I:%M %p on %B %d, %Y  (UTC format)")
    with open(logintxt, 'a') as pi:
        pi.write(f"\n{name} (Guest) logged in at {time}\n")
    print("Logged in as a guest.")
def view_log():
    logintxt = "/mnt/student/MuskHub/data/lognardx.txt"
    with open(logintxt, 'r') as file:
        time.sleep(0.2)
        print("\033c")
        print("\n"*20)
        content = file.read()
        print(content)
        print("\n"*7.5)
        input("""Done yet? (press any key) 
                   """)
def main(de, m):
    admin = False
    member = False
    creator = False
    banned = False
    impersonating = False
    guest = False
    
    print("\n"*100)
    print("\033c")
    print("\n"*100)
    
    logo = """\033[1;32m
 ____    ____                 __       ____  ____   🐍     __        
|_   \  /   _|               [  |  _  |_   ||   _|  1.0    [ |       
  |   \/   |  __   _   .--.   | | / ]   | |__| |  __   _   | |.--.   
  | |\  /| | [  | | | ( (`\]  | '' <    |  __  | [  | | |  | '/'`\ \ 
 _| |_\/_| |_ | \_/ |, `'.'.  | |`\ \  _| |  | |_ | \_/ |, |  \__/ | 
|_____||_____|'.__.'_/[\__) )[__|  \_]|____||____|'.__.'_/[__;.__.'  
\033[0m
    """
    
    centered_logo_lines = [line.center(115) for line in logo.split('\n')]
    centered_logo = '\n'.join(centered_logo_lines)
    print(centered_logo)
    
    while True:
        print("\n")      
        choice = input("\t\t\t\t\t1. Sign Up 2. Login 3. Be Guest ")        
        if choice == "1":
            username = input("\nUsername: ")
            display_name = input("Display Name: ")
            password = input("Password: ")
            level = "user"
            ask = input("Do you want to make this an admin account? (y/any key): ")           
            if ask.lower() == "y" or ask.lower() == "yes":
                level = input("Enter secret admin code: ")
                if level == de:
                    level = "admin"
                    admin = True
                    print("You're now admin!")
                elif level == m:
                    level = "creator"
                    creator = True
                    print("WOW! You're now the official founder of MuskHub!\nGreat to see you!")
                else:                    
                    print("Wrong code :(")
            else:
                print()
            signup(username, password, display_name, level)
            
        elif choice == "2":
            username = input("\nUsername: ")
            password = input("Password: ")
            login(username, password)
        elif choice == "3":
            name = input("\nUsername: ")
            guester(name) 
            guest = True
        else:
            print("Invalid choice :( Try again")
    
    if guest:
        ru = input("\t\t\t\t\tView rules and instructions? (y/any key): ")
        if ru.lower() == "y" or ru.lower() == "yes":
            print(rules)
            input("Understand? (press enter): ")
            time.sleep(0.4)
            print("\033c")
            while True:
                print(guest_menu)
                en = input("\n\t\t\t\t\tEnter menu option number (1-n2): ")
                time.sleep(0.2)
                print("\033c")                
        else:
            print("\n"*15)
            while True:
                print(guest_menu)
                en = input("\n\t\t\t\t\tEnter menu option number (1-n2): ")
                time.sleep(0.2)
                print("\033c")    
    elif admin:
        uo = input("\t\t\t\t\tView rules and instructions? (y/any key): ")
        if uo.lower() == "y" or uo.lower() == "yes":
            print(rules)
            input("Understand? (press enter): ")
            time.sleep(0.4)
            print("\033c")
            while True:
                print(admin_menu)
                ni = input("\n\t\t\t\t\tEnter menu option number (1-n2): ")
                time.sleep(0.2)
                print("\033c")                
        else:
            print("\n"*15)
            while True:
                print(admin_menu)
                kj = input("\n\t\t\t\t\tEnter menu option number (1-n2): ")
                time.sleep(0.2)
                print("\033c")    
    elif member:
        uom = input("\t\t\t\t\tView rules and instructions? (y/any key): ")
        if uom.lower() == "y" or uom.lower() == "yes":
            print(rules)
            input("Understand? (press enter): ")
            time.sleep(0.4)
            print("\033c")
            while True:
                print(member_menu)
                nm = input("\n\t\t\t\t\tEnter menu option number (1-n2): ")
                time.sleep(0.2)
                print("\033c")
        else:
            print("\n"*15)
            while True:
                print(member_menu)
                mj = input("\n\t\t\t\t\tEnter menu option number (1-n2): ")
                time.sleep(0.2)
                print("\033c")

NOTE I already have the member_menu and the other menu vars, and the import statements at the very top of the code (not listed here).

I need some help asap plz D:

The while True loop in your main() function never ends… Did you forget to put break statements?

For the guest if statement, could you clarify what the problem is?

Also, I’d recommend using just a single lvl variable in main() instead of a bunch of boolean variables.

1 Like

I wanted that so i could have different menu layouts for different lvls.

I tried the break statements but it still doesn’t work.
Also, how could i have the admin and member = True in the functions and not in the main() function?

Can you clarify? Is it not possible to use a single lvl variable?

You can put a break at the end of the entire while loop, but put continue where the user must try again.

I am not sure what you mean. A level variable can be used instead of any boolean admin or member variables, if I am not misinterpreting anything.

no. i want different levels like admin member and guest to feature different menu layouts and powers.

ok.

I still do not understand. Why use 3 different boolean variables for 3 different states, when you can use one variable for the same 3 states?

lvl = "admin"
if lvl == "guest":
  guest_menu()
elif lvl == "member":
  member_menu()
elif lvl == "admin":
  admin_menu()
3 Likes

Thanks!
I wil try them.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.