• Register
2 votes
37 views

Problem:

Hello guys,

I’ve written a chunk of code in python based on functions. However, my simple program is not working and throwing an error. Being a new student to the programming world I’m unable to figure out the problem and fix it. Please, find my sample code below and fix the error for me.

n = str(input("Enter a number here: "))
if(n == '1'):
    one()
elif (n == '2'):
    two()

def one():
    print('This is function one')

def two():
    print('This is function two')

And the error I’m getting:

NameError: name 'one' is not defined

Here, I am trying to get input and compare it with my variable. The matched case will print the function.
How can I fix the issue? Thanks for your concern.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

2 votes

Solution:

In your case, the NameError happens because you didn't define your function before use/call it. A python program is read from top to bottom by the compiler. Once the compiler getting a function call without defined, it throws back the error. To fix the error, you need to define the functions before using them in your program and only then the compiler will understand what to do with those functions. I guess you want to do something like:

def one():
    print('This is function One')

def two():
    print('This is function Two')

n = str(input("Enter num To Start FunctionOne"))
if(n == '1'):
    one()
elif (n == '2'):
    tow()

Good Day!

13 9 6
94,260 points

Related questions

0 votes
1 answer 27 views
27 views
Peroblem: How can I fix the issue? Thanks for your concern:Nameerror name is not defined python 3 .
asked Mar 15 Wafa Abu Yousef 6.1k points
0 votes
1 answer 411 views
411 views
Problem Hello Kodlogs, My code is giving me nameerror: name 'unicode' is not defined. Here is the code # utf-8 ? we need unicode if isinstance(unicode_or_str, unicode): text = unicode_or_str decoded = False else: text = unicode_or_str.decode(encoding) ... if isinstance(unicode_or_str, unicode): NameError: global name 'unicode' is not defined It started happening on Python 3. Please help
asked Nov 3, 2020 hashq 1.1k points
0 votes
1 answer 33 views
33 views
Problem: How to I Solve this: python nameerror name is not defined function HELP! I need help understanding this: python nameerror name is not defined function Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 5 Mashhoodch 13k points
0 votes
1 answer 123 views
123 views
Am encountering this error in Windows Command line,done a wide search but could not get a perfect answer.Please find the error below and help in solving. python Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'python' is not defined >>> Thanks ... 31 '13 at 12:21 you did not include the >>> before the word 'python', right? &ndash; Elazar May 31 '13 at 12:23
asked Oct 25, 2020 psandprop 2.4k points
0 votes
1 answer 26 views
26 views
Problem: I need to know what is the problem: traceback (most recent call last): NameError: name '&hellip;' is not defined
asked Mar 19 Wafa Abu Yousef 6.1k points
0 votes
1 answer 11 views
11 views
Problem: Any help: traceback (most recent call last): NameError: name '&hellip;' is not defined
asked Mar 19 Wafa Abu Yousef 6.1k points
0 votes
1 answer 68 views
68 views
Problem: could anybody help me ! NameError: name 'pd' is not defined.
asked Mar 6 Wafa Abu Yousef 6.1k points
0 votes
1 answer 18 views
18 views
Problem: How to I Solve this: nameerror: name 'include' is not defined HELP! I need help understanding this: nameerror: name 'include' is not defined Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 3 Mashhoodch 13k points
0 votes
1 answer 580 views
580 views
I've just installed python 3.5, ran Python 3.5 (32-bit) and typed pip and received the message: Traceback (most recent call last): File "<pyshell#2>", line 1, in <module> pip NameError: name 'pip' is not defined I don't see any scripts directories in my path, ... Scripts folder from above, same result. How do I install python so that it actually works (ie. I can run pip, install modules, etc.)?
asked Oct 25, 2020 psandprop 2.4k points
0 votes
2 answers 113 views
113 views
There has been an error when trying to run this script : input_variable = input ("Enter your name: ") print ("your name is" + input_variable) Let's say I type in "dude", the error I am getting is: line 1, in <module> input_variable = input ("Enter your name: ") File "<string>", line 1, in <module> NameError: name 'dude' is not defined I am running these scripts with Python 2.7.
asked Oct 24, 2020 psandprop 2.4k points