Throw NameError: name 'person' is not defined in Python 3.x

Dung Do Tien Oct 01 2021 3

Hello you guys, I am a newbie in Python and I'm also studying more about Python.

I have some lines of code, I want to learn more about function and how to call a function in Python. Function as below:

long = int(input("Enter first number:"))
short = int(input("Enter second number:"))

person()

def person(): 
    if short == 0 and long > 0:
        print("You are long")
    elif short == 1 and long > 0:
        print("You are normal")
    elif long < 1:
        print("You not long")
    long =- 1

But I get an exception NameError: name 'person' is not defined when I run code above.

Enter first number:9
Enter second number:0
Traceback (most recent call last):
  File "main.py", line 4, in <module>
    person()
NameError: name 'person' is not defined

And I am using python 3.8.2

Anyone can explain it to me? How can I solve it?

Thanks for any suggestions.

Have 1 answer(s) found.
  • W

    Waleed Ali Oct 01 2021

    You need to declare/create function before you calling it.

    long = int(input("Enter first number:"))
    short = int(input("Enter second number:"))
    
    #Step 1: Define function
    def person(): 
        if short == 0 and long > 0:
            print("You are long")
        elif short == 1 and long > 0:
            print("You are normal")
        elif long < 1:
            print("You not long")
        long =- 1
    
    #Step 2: Call function (You need to call after defined it.)
    person()

    Note: Python not same with Javascript. Javascript allow using before define but Python is not.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close