Python dumb newbie question about def

  • Thread starter funcosed
  • Start date
  • Tags
    Python
In summary, the conversation discusses a newbie who is learning Python and needs help creating a function that can be called from the Python shell. The person has written a script but encounters an error when trying to call the function. They realize that they haven't properly imported the module and are provided with instructions on how to do so. The summary ends with the acknowledgement that the solution worked.
  • #1
funcosed
37
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
  • #2
Please use [code ] tags...
Code:
[color=#408080][i]#!/usr/bin/python[/i][/color]
[color=#008000][b]def[/b][/color] [color=#0000FF]examplr[/color]([color=#008000]str[/color]):
    [color=#008000][b]print[/b][/color] [color=#008000]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:
[color=#008000][b]import[/b][/color] [color=#0000FF][b]example[/b][/color]
to bring the module into the current namespace. Then, you can access you function as
Code:
example[color=#666666].[/color]example([color=#BA2121]'foo'[/color])
 
  • #3
Thanks, that worked I hadnt imported it properly.
 

1. What is the purpose of using "def" in Python?

The "def" keyword is used to define a function in Python. It allows you to create a block of code that can be called and executed multiple times throughout your program.

2. How do I use "def" to create a function in Python?

To create a function in Python, you use the "def" keyword followed by the function name and a set of parentheses. Inside the parentheses, you can specify any parameters that you want to pass into the function. After the parentheses, you use a colon to start the function body, which is indented to indicate that it is part of the function.

3. What is the difference between "return" and "print" in a function?

The "return" keyword is used to specify the value that a function should give back to the caller when it is called. This value can be stored in a variable or used in other operations. On the other hand, "print" is used to display a value on the screen, but it does not return any value to the caller. In most cases, you will use "print" for displaying information and "return" for returning a value from a function.

4. Can I have multiple "return" statements in a function?

Yes, you can have multiple "return" statements in a function. However, as soon as the first "return" statement is executed, the function will stop and return the specified value to the caller. Any code after the "return" statement will not be executed. This is why it is important to carefully design the logic of your function and ensure that all possible paths have a "return" statement.

5. Can I call a function inside another function in Python?

Yes, you can call a function inside another function in Python. In fact, this is a common practice in programming, as it allows you to break down complex tasks into smaller, more manageable functions. Just make sure that you have defined the function you want to call before using it in another function.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
166
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
3
Views
267
  • Programming and Computer Science
Replies
9
Views
2K
Replies
3
Views
718
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
2
Replies
43
Views
2K
  • Programming and Computer Science
Replies
1
Views
640
  • Programming and Computer Science
Replies
1
Views
2K
Back
Top