Python Forum
Defining a function with input - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Defining a function with input (/thread-32460.html)



Defining a function with input - abcd - Feb-10-2021

Hi,

In a new file, I typed that:
def CircleArea():
    radius = input('Type the circle radius')
    return radius * 3.14
Then, I ran the module and typed CircleArea in the Shell but it just gave me <function CircleArea at 0x02A138E8> but not proposed me to enter the radius as I wanted.

How to remedy this please ?

Thank you!


RE: Defining a function with input - BashBedlam - Feb-10-2021

First, radius is going to be a string and you want an integer. Try this:

def CircleArea():
	radius = int (input ('Type the circle radius'))
	return radius * 3.14

print (CircleArea ())



RE: Defining a function with input - perfringo - Feb-10-2021

(Feb-10-2021, 10:15 PM)BashBedlam Wrote: First, radius is going to be a string and you want an integer.

It's correct that input returns str but radius can be integer as well as float.

For OP I suggest to follow conventions set in PEP8 for function naming (circle_area).

Regarding original question:

>>> def my_func():
...     print('This is my_func')
...
>>> my_func
<function my_func at 0x7fce15136c10>
>>> my_func()
This is my_func



RE: Defining a function with input - deanhystad - Feb-10-2021

CircleArea is a function. CircleArea() calls the function and returns the result.

You are probably wondering why you need the parenthesis to call the function. That is just how Python works. When Python encounters something that is not a keyword (if, else, while...), number (2, 3.14, 0xff, 0b1010), list([]), string(''), dictionary ({}) ... it looks for the name to be defined somewhere. The name might be a variable, or the name might be a function. If python cannot find the name it raises a NameError exception.

If python finds the name it returns the object that is bound to the name. In your example you have a name "CircleArea" that is bound to a function. When you print the function you get that "<function CircleArea at 0x02A138E8>" printed out. If you want to call the function you need to follow the name with parenthesis, possibly containing arguments to be passed to the function.
def cube(x):
    return x**3

print(cube)
print(cube(3))
x = cube
print(x(4))
Output:
<function cube at 0x000001D188108280> 27 64
Notice that I can reference the function using a different name. In this case I told Python that I wanted to use the name "x" to reference the function cube. If I follow "x" with parenthesis it calls the function the same way as if I used the name "cube".


RE: Defining a function with input - abcd - Feb-20-2021

Alright, thank you all for your answers! Smile


RE: Defining a function with input - NullAdmin - Feb-21-2021

im fairly intermidiate, but i still know the answer. this is the original code:
def CircleArea():
    radius = input('Type the circle radius')
    return radius * 3.14
first thing that the radius variable in string, but it needs to be an integer variable, so a simple fix is like this:
def CircleArea():
	radius = int(input('Type the circle radius'))
	return radius * 3.14
also, if you forgot, add parenthses if you didnt already to the end of CircleArea, like this:
CircleArea()
.
now it should work fine, works at least for me.