0

How would I round 'com' in this Python code?

def percentage(com,rd):
    apl = [10,10,10,5,5,20,10,10,10,10]
    if age > 15:
        t=sum(apl)
    if age < 15:
        t=sum(apl) + 10
    com=rd/t*100
    *round(com,0)*
    return com
aIKid
  • 26,968
  • 4
  • 39
  • 65
Yumi H
  • 129
  • 1
  • 10

3 Answers3

4

Pretty fine, except that you have to store the returned value somewhere:

def percentage(rd):
    apl = [10,10,10,5,5,20,10,10,10,10]
    if age > 15:
        t=sum(apl)
    if age < 15:
        t=sum(apl) + 10
    com=rd/t*100 #this overwrites whatever the passed argument com was.
    com = round(com,0) #assign the rounded value back to com
    return com

Also, you don't actually need com as a parameter, since you assigned it in the function.

Hope this helps!

aIKid
  • 26,968
  • 4
  • 39
  • 65
3
com = round(com,0)

round is a function that just returns a value, does not modify the variable you provide it. You have to assign that value yourself.

Amadan
  • 191,408
  • 23
  • 240
  • 301
0

You probably want the round function. Say you wanted to round it to the nearest ten:

>>> i = 222
>>> print round(i, -1)
220.0
Christian Ternus
  • 8,406
  • 24
  • 39