-2

This is another bit of my code that I posted in another question that has been giving me trouble.

I need to set the result of a function equal to a variable.

Let's say I have a function that divides pizza of x slices between y people.

def pizzaFunction(x,y):
    return x/y

Simple enough. But now say I need to print that into a statement later so that the statement says

print("The number of slices per person is",slice_number)

So my question is, since

slice_number = pizzaFunction(x,y)

does not work, is there another way to do this? It is important that it be saved into some variable so that it can be used in a print statment later.

Any and all help is appreciated.

The exact error I'm getting when I try the above method is

NameError: "name 'slice_number' is not defined"
user2977230
  • 7,973
  • 4
  • 13
  • 9
  • 7
    Why doesn't `slice_number = pizzaFunction(x, y)` work? – Ismail Badawi Dec 01 '13 at 06:57
  • 1
    @IsmailBadawi My question exactly. – Games Brainiac Dec 01 '13 at 06:58
  • http://stackoverflow.com/questions/750136/python-how-to-return-something-from-a-function-that-makes-a-dictionary – CodeMed Dec 01 '13 at 06:59
  • It should work... Does it throw an error? – aIKid Dec 01 '13 at 06:59
  • @IsmailBadawi I wish I knew, but when I try I get the error "name 'slice_number' is not defined" – user2977230 Dec 01 '13 at 07:00
  • 1
    Are you doing `print("The number of slices per person is", slice_number = pizzaFunction(x,y))`? – Burhan Khalid Dec 01 '13 at 07:01
  • Copy paste things that happened there to here.. – aIKid Dec 01 '13 at 07:01
  • 1
    Show us the non-working code. My guess is a scope error; you assigned to `slice_number` inside a function, expecting that assignment to affect a global `slice_number` variable instead of creating a local variable. – user2357112 Dec 01 '13 at 07:03
  • The non working code is much longer but is a homework assignment, and I feel I'd be cheating my class and my self if I got answers _too_ specific. The above code is a simple microcosm of my non-working code. Basically, the variable is set equal to a different function depending on which if-statement is true. – user2977230 Dec 01 '13 at 07:28

2 Answers2

1

How you're doing it seems to work fine.

Example:

def Pizza(x,y):
    return x/y

z = Pizza(12,3)

print("%i slices per person" % z)

Produces:

4 slices per person
K DawG
  • 12,572
  • 9
  • 32
  • 65
MoJi
  • 88
  • 6
  • Maybe it's because I have multiple functions that would return the same variable in an if/then series. So depending on which if statement is true, the function used changes. – user2977230 Dec 01 '13 at 07:07
  • @user2977230: "multiple functions that would return the same variable" - functions return objects, not variables. It doesn't matter what name a function uses to refer to the thing it returns; the code calling the function can't tell whether the return value used to be called `result` or `foo` or `roasted_walnuts`. I suggest you read up on the scope rules. – user2357112 Dec 01 '13 at 08:02
0

I assume, "does not work" might refer to a integer division "error": Python might consider x as an integer, divides by y and returns the result as an integer as well (which means that all decimal digits are cut)

use return float(x)/y in your function.

If this is not the problem, I strongly assume a typo or indentation error in your code. Or you define your function after first usage.

OBu
  • 4,661
  • 2
  • 26
  • 43