Python Forum
Defining a function with input
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Defining a function with input

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!
buran write Feb-11-2021, 08:47 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
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 ())
(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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
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

x = cube
<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".
Alright, thank you all for your answers! Smile
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:
now it should work fine, works at least for me.

Possibly Related Threads…
Thread Author Replies Views Last Post
  Input function cutting off commands at spaces. throwaway34 3 409 May-12-2021, 06:40 AM
Last Post: throwaway34
  Function with array input Gerkamspiano 5 576 Dec-01-2020, 08:48 AM
Last Post: Gerkamspiano
  calling a function and argument in an input phillup7 3 709 Oct-25-2020, 02:12 PM
Last Post: jefsummers
  Use of input function to change screen background color in Turtles Oldman45 3 1,412 Jul-10-2020, 09:54 AM
Last Post: Oldman45
  Hi, I need help with defining user's input and applying it to code. jlmorenoc 2 755 Jun-24-2020, 02:10 PM
Last Post: pyzyx3qwerty
  Getting an error while using input function dcsethia 5 871 May-11-2020, 04:59 PM
Last Post: buran
  When Defining a Function with an Equation as a Default Argument, which Value Is Used? OJGeorge4 4 875 Apr-09-2020, 08:48 AM
Last Post: DeaD_EyE
  Conjugate Gradient having issues with defining A (function to solve [A]{x} = {b} ) DimosG 1 818 Apr-07-2020, 09:03 PM
Last Post: micseydel
  Input() function not working in VS Code darpInd 7 2,122 Feb-17-2020, 03:28 PM
Last Post: snippsat
  Using input function in a Class wew044 2 814 Feb-06-2020, 03:08 AM
Last Post: wew044

Forum Jump:

User Panel Messages

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