Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help To Get Started
#1
Dear member,

In ..\python\python 313-32\ I created cAdd.py (See first attachment)

With IDLE I tryed to run cAdd.py, but I got a NameError : cAdd is not defined. (See second attachment)

How do I solve the NameError?

Yours faithfully.

HansieB

Attached Files

Thumbnail(s)
       
Reply
#2
You are trying to call the function cAdd in the interactive python shell. Where it is not defined, unlike names u and v, which you define on the first two lines.
If you want to run the cAdd,py, go to File->Open and open the module. Then Run->Run Module
Note that in this case you will get NameError for a and b, which are not defined in the module, before line cAdd(a, b)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Nov-29-2024, 10:39 AM)buran Wrote: Note that in this case you will get NameError for a and b, which are not defined in the module, before line cAdd(a, b)

Dear buran,
Thank you for your answer.

Where can I define a (and b)?

Regards.

HansieB
Reply
#4
Something like
# define the function
def cAdd(a, b): # this name is not PEP8 compliant
    return [a[0] + b[0], a[1] + b[1]] 

# define names (variables) you will pass as aruments
u = [1, 2]
v = [3, 4]

# call the function, assign the return value to name result
result = cAdd(u, v)

# print result
print(result)
Note there are plenty of other ways to do this, e.g.
# define names (variables) you will pass as arguments
u = [1, 2]
v = [3, 4]

# use list comprehension to iterate over zipped lists, assign the list to name result
result = [a + b for a, b in zip(u, v)]

# print result
print(result)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(Nov-29-2024, 10:39 AM)buran Wrote: You are trying to call the function cAdd in the interactive python shell. Where it is not defined, unlike names u and v, which you define on the first two lines.
If you want to run the cAdd,py, go to File->Open and open the module. Then Run->Run Module
Note that in this case you will get NameError for a and b, which are not defined in the module, before line cAdd(a, b)
Reply
#6
Thank you buran!

Have a nice weekend!

HansieB
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Cannot get started standenman 4 2,257 Feb-22-2023, 05:25 PM
Last Post: standenman
  Can't even get started dr6 1 2,115 Aug-18-2020, 04:38 PM
Last Post: Larz60+
  Getting started mba_110 0 2,122 Jan-18-2019, 05:23 PM
Last Post: mba_110
  Getting Started wargasme 5 4,446 Jun-19-2018, 07:25 PM
Last Post: ichabod801
  Just getting started alwillia 6 4,732 May-20-2018, 07:22 PM
Last Post: ljmetzger

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020