1

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.

Andy
50.9k62 gold badges181 silver badges240 bronze badges
asked Apr 29, 2015 at 3:16
3
  • 1
    Your string formatting syntax is incorrect, along with some other some other bits like your use of semicolons. Commented Apr 29, 2015 at 3:19
  • Well, the string formatting (or lack thereof) could be correct with print(*output) instead of print(output). But still, not the clearest way to do it by any means... Commented Apr 29, 2015 at 3:38
  • Thank you guys for the input, blame my professor for the semicolons she insists we have to use them and as such I just always include them Commented Apr 29, 2015 at 17:55

4 Answers 4

1

Ok, you've got a few different issues at play here.

  1. You don't return anything from function fgp: the return; at the end of fgp returns None, which in Python, indicates the absence of a value. Don't want that! Instead, use: return fgp.
  2. You're not calling fgp in fgpoutput - 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.
  3. They way you construct output isn'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!

answered Apr 29, 2015 at 3:23
Sign up to request clarification or add additional context in comments.

1 Comment

Your point 3 is wrong. In Python 3 (which the OP is clearly using, given the input calls), dividing two ints returns a float. But otherwise, good explanations of everything else.
1

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.

answered Apr 29, 2015 at 3:22

Comments

0

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), "%"
answered Apr 29, 2015 at 3:22

Comments

0

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

answered Apr 29, 2015 at 4:06

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.