Python Why Does My Python Function Give a NameError in the Shell?

  • Thread starter Thread starter funcosed
  • Start date Start date
  • Tags Tags
    Python
AI Thread Summary
To create a callable function in Python, ensure that the function is defined correctly and the module is imported into the Python shell. In the provided script, the function is named "examplr," which should be called as "examplr('ddd')" after importing the module. If using IDLE, the script should be run with F5, or in the shell, navigate to the directory of the script and use "import example" to access the function. The error "NameError: name 'example' is not defined" occurs when the module is not imported correctly. Properly importing the module resolves the issue.
funcosed
Messages
35
Reaction score
0
Newbie question...
I'm just learning python and need some help with creating functions. I want to create a function that is callable from python shell. I write a script as follows,

#!/usr/bin/python
def examplr(str):
print str

save it as example.py, then in shell try example('ddd') but it returns an error,
NameError: name 'example' is not defined. Anyone know what I am doing wrong?
 
Technology news on Phys.org
Please use [code ] tags...
Code:
[i]#!/usr/bin/python[/i][/color]
[b]def[/b][/color] examplr[/color](str[/color]):
    [b]print[/b][/color] str[/color]
Based on your code, you've named your function examplr.

If that's just a typo in your post, and you're correctly calling the function, then it sounds like you haven't imported your module into Python. If you're using IDLE, hit F5 to run the script. Otherwise, run the interpreter in the directory you've saved example.py and run
Code:
[b]import[/b][/color] [b]example[/b][/color]
to bring the module into the current namespace. Then, you can access you function as
Code:
example.[/color]example('foo'[/color])
 
Thanks, that worked I hadnt imported it properly.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
18
Views
1K
Replies
10
Views
1K
Replies
15
Views
2K
Replies
3
Views
1K
Replies
4
Views
5K
Back
Top