• Register
2 votes
28 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,240 points

Related questions

0 votes
1 answer 125 views
125 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 75 views
75 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 207 views
207 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 78 views
78 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
1 vote
1 answer 125 views
125 views
Problem: I was doing a project work where I had to use pandas and numpy. I wrote a python program with them: data =pandas.read_csv('valla',nov=',') data=data.values P=np.ones((13,56)) Y=data[:,45] for i in range (1,12): X= np.column_stack((P,(dat[:,i])) ... problem is with the file I was trying to read? If there is any thing wrong in my code what is that and how to solve that. Thanks in advance.
asked May 17, 2020 samhaz 5k points
1 vote
1 answer 225 views
225 views
Solution: I am a complete beginner in python. Some of my friends inspired me to learn computer programming and suggested me the python language. I am stepping ahead by taking help from YouTube videos. Right now I have written a python program to calculate some basic things ... issue? Where I am doing the wrong things? Could you please figure it out? Thanks for your kind concern Thanks in advance.
asked May 9, 2020 adamSw 11.3k points
1 vote
1 answer 365 views
365 views
Problem: Hello all, I am new in python programming as well as this forum. I've enrolled in an online python programming course to develop my knowledge and skill. It is going quite well. However, I&rsquo;ve written something very basic using the python language but the ... ;ve searched the problem but unable to figure it out. I am hoping a legitimate solution. Thanks in advance for your kind help.
asked May 9, 2020 adamSw 11.3k points
1 vote
1 answer 195 views
195 views
Problem: Hello Good people, I am outrageously new in python and helping me tech python. I am learning the python language by seeing some YouTube channels and from many tutorial-based blogs over the internet. I am reading each topic and practicing the topic by solving some ... /") for file in all_files: print file I am looking forward to hearing from you guys. Thanks for your kind concern. Thanks.
asked May 9, 2020 adamSw 11.3k points
3 votes
2 answers 323 views
323 views
Problem: I'm new to Python. Would you please tell me what's wrong with the following code? When I run it, I got an error message of "NameError: global name 'reduce' is not defined". I asked Google but it didn't work for me. There are some solutions but ... see anything weird in the snippet? Please help me out to fix the issue. I am looking forward to having a legit solution to this. Thanks.
asked May 6, 2020 Gavin 15.3k points
2 votes
1 answer 935 views
935 views
Problem: Hello guys, I have a piece of code which is working in Linux, and I am now trying to run it in windows, I import sys but when I use sys.exit(). I get an error, sys is not defined. It&rsquo;s a 200 lines code almost, so I can&rsquo ... googling for 30 minutes I&rsquo;ve got nothing about this spooky error. I am looking for a feasible solution to this. I appreciate your concern. Thanks
asked May 6, 2020 Gavin 15.3k points