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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

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