1

I'm new to Python. I'm currently reading the book "How to Automate The Boring Stuff". I'm currently creating my first project. I'm creating a user login. I just got into the "def" define chapter. So I just implement it to my code. I can't figure out why my while loop won't break at "else". Sorry if my code looks very noobish. I'm a beginner and trying to learn from my mistakes.

P.S - This is my first post ever. Sorry if I'm not properly wording my question correctly.

edit - I've been creating this project for 3 days now.

import time
print("Welcome to my first User Login!")
time.sleep(1)


def creating_account():
    print("First you will need to create a username!")
    time.sleep(0.5)
    asking_username = input("\nUsername: ")
    time.sleep(0.5)
    asking_password = input("Password: ")
    time.sleep(1)
    print("\nPerfect you have now created a User Login!")
    time.sleep(1)
    print("\nI will need your login information.")
    
    login_username = asking_username
    login_password = asking_password

    userlogin_username = input("\nUsername: ")
    userlogin_password = input("Password: ")

    while True:
        if userlogin_username != login_username or userlogin_password != login_password:
            time.sleep(0.5)
            print("\nSorry your username or password doesn't match with what We have on file. You may try again.\n")
            userlogin_username = input("\nUsername: ")
            userlogin_password = input("Password: ")
            
        elif userlogin_username == login_username and userlogin_password == login_password:
            break

    print("\nProcessing..")
    time.sleep(1.5)
    print("\nYou have successfully login!")
            
asking_account = input("Do you have an account (y/n)? ").lower()

if asking_account == "n":
    creating_account()

elif asking_account == "y":
    print("Not possible, You must create an account.\n")
    time.sleep(1)
    creating_account()


else:
    print("\nSorry, I don't understand. Make sure you're typing \"y\" or \"n\".\n")
    time.sleep(0.5)
    while True:
        asking_account = input("Do you have an account (y/n)? ").lower()
        if asking_account != "y" or "n":
            print("\nSorry, I don't understand. Make sure you're typing \"y\" or \"n\".\n")
            time.sleep(0.5)
            
        elif asking_account == "y" or "n":
            break
    creating_account()
Ominpathy
  • 49
  • 5

2 Answers2

2

This is really good code for a first-timer! You could go far if you keep working on your skills.

I think I've spotted a mistake on Line 53 that's causing the error.

The code says:

        if asking_account != "y" or "n":

but it should read:

        if asking_account != "y" and asking_account != "n":

"or" in python doesn't quite work the way you'd think it would: It determines if either of two True/False questions are correct, eg.

if YOU_ARE_MALE or YOU_ARE_OVER_18, then you are not a girl.

JimmyCarlos
  • 1,934
  • 1
  • 10
  • 24
1

@Jimmy Carlos is right in pointing out the mistake with your code. I would, however, like to add by saying that:

  1. I see the indentation of your function call creating_account() on the last line of code is not correct. Not sure if that's how it's on your code or if it's wrongly formatted while asking here. But that is one thing I spotted.

  2. It's a good practise enclosing conditions within () while working with logical operators. Holds true for other operator types that weigh in operator precedence Especially when you will have complex conditions inside your if

  3. Also, get into the habit of writing conditions within () for if statements as well. It helps with readability and clean code. I know you can work without it since python gives us leeway but just some 2 cents for a beginner.

I hope it helps.

  • Thanks for tips! I checked my indentation. I'm not sure If I'm suppose to indent it more because it will go in my while loop. Also the program executes the function after the while loop break. I kinda don't understand what you mean by "enclosing conditions within '()' " I tried to understand why people write conditions within "()". I still don't fully understand it yet. Sorry I'm really new ); – Ominpathy Jul 06 '20 at 06:13
  • 1
    Keep your chin up and keep learning brother. And remember to help others. I am so happy whenever I find a kind and helping hands like you and others in this post that I try my best to help out. –  Jul 06 '20 at 06:14
  • Sorry that I suck at understanding. I usually learn from examples, then doing it myself. – Ominpathy Jul 06 '20 at 06:19