• learning functions.

    can someone please run this and let me know if I'm being long winded / over complicating things or if it is OK?

    I'm sure I've already got some bad habits so would really appreciate some constructive feedback!

    import getpass
    
    actual = {"username": "", "password": ""}
    attempt = {"username": "", "password": ""}
    
    def actual_credentials():
        print("Choose username:")
        username=input()
        actual["username"] = username
        print("Choose password:")
        password=getpass.getpass()
        actual["password"] = password
        
    def attempt_credentials():
        print("Enter username:")
        username=input()
        attempt["username"] = username
        print("Enter password:")
        password=getpass.getpass()
        attempt["password"] = password
    
    def main():
        exit=False
        while exit is False:
            word=input("Continue typing, lock (l) or quit (q)? ")
            if word!="l" and word!="q":
                exit=False
            elif word=="l":
                x=False
                while x is False:
                    attempt_credentials()
                    if actual["username"]!=attempt["username"] or actual["password"]!=attempt["password"]:
                        print("Username or password are incorrect")
                        x=False
                    else:
                        print("You're in!")
                        x=True
            elif word=="q":
                print('Thanks, goodbye!')
                exit=True
    
    actual_credentials()
    main()
    
  • I'm not a python dev and I don't really know what I'm doing but here are my thoughts.

    Long nested loops like in your main function get really hard to follow very quickly. Ideally you never want to go deeper than 1 loop. Try and keep your variable names meaningful too, 'x' isn't a great name. In line 26-27 you're catering for a case where nothing happens, so you don't need to cover it.

    Attached my small refactor


    1 Attachment

About

Avatar for Grimm @Grimm started