I'm relatively new to python and coding in general. I've searched through some of the other similar questions already cannot seem to find the answer for what I'm looking for. I'm working on a small program that will calculate a basketball players Offensive Efficiency, yet when I define a program and call it back it does not produce a value.
def fgp(x, y):
fgp = (x/y)*100
return;
def fgpoutput():
x = int(input("How many shots made?"));
y = int(input("How many shots attempted?"));
fgp(x, y);
output = "This player is shooting",fgp,"%"
print(output)
fgpoutput()
This seems to work I think, but I cannot tell because it returns this:
How many shots made?5
How many shots attempted?10
('This player is shooting', function fgp at 0x02561858, '%')
I feel like I've gotten close, but cannot seem to nail it.
4 Answers 4
Ok, you've got a few different issues at play here.
- You don't return anything from
function fgp: thereturn;at the end offgpreturnsNone, which in Python, indicates the absence of a value. Don't want that! Instead, use:return fgp. - You're not calling
fgpinfgpoutput- you're simply printing the function itself. Instead, you want to call the function like this:fgp(x, y), which now returns the calculated value. - They way you construct
outputisn't quite right. In Python, there's a string method for formatting strings to include numbers:str.format(). Check out the documentation on it here.
So, altogether we get:
def fgp(x, y):
fgp = (x/y)*100
return fgp
def fgpoutput():
x = int(input("How many shots made?"));
y = int(input("How many shots attempted?"));
output = "This player is shooting {} %".format(fgp(x, y))
print(output)
fgpoutput()
Overall though, you're definitely on the right track. Good luck!
1 Comment
output = "This player is shooting",fgp,"%" is printing the "fgp" function itself, not what it is calculating. What you are probably looking for is:
def fgp(x, y):
return (x / y) * 100
That will return the value you want to calculate. Then you can call it in your output:
def fgpoutput():
x = int(input("How many shots made?"))
y = int(input("How many shots attempted?"))
result = fgp(x, y)
output = "This player is shooting {} %".format(result)
print(output)
Also, you don't need the semicolons at the end of the lines in Python.
Comments
A function is, as everything else in Python, an object. When printing fgp, you are printing a reference to the executable function object.
But, differing to Matlab, to return from a function in Python, you actually have to return the value:
def fgp(x, y):
return (x/y) * 100
If you want to call fgp, you'll have to execute it:
output = "This player is shooting", fgp(x, y), "%"
Comments
There appear to be a few problems
First the function -
def fgp(x, y):
fgp = (x/y) * 100
return;
In the function above you are using 'fgp' again inside the function 'fgp'. That per se is not a problem (the function fgp is a global while the fgp inside function fgp is local), but it's extremely unreadable and confusing. You should use something like 'f' if you have to.
Second -
(x/y) * 100
This function will almost always return 0 (if both x and y are integers and in your case they are and x < y). That line should look something like
(x * 100.0) / y # force the numerator to a float
So your fgp becomes
def fgp(x,y):
assert y is not 0
return (x * 100.0/y)
Let it raise an AssertionError if y happens to be zero. This shoud be it
print(*output)instead ofprint(output). But still, not the clearest way to do it by any means...