I am new to python! Done my studying, gone through several books and now attempting pyschools challenges. Done Variables and data types successfully but Question 7 of Topic 2 (Functions) is giving me hell.
I am using Eclipse with Python (ver 3.2). in my eclipse, I get the answers 100, 51 and 525. Those are the same answers pyschools expects but it shows that my function returns 100, 0 and 500.
Here is the question (Hope am allowed to post it here!):
Write a function percent(value, total) that takes in two numbers as arguments, and returns the percentage value as an integer.
And below is my function
def percent (value, total):
a = value
b = total
return(int((a / b) * 100))
percent(70, 70)
percent(46, 90)
percent(63, 12)
Can anyone tell me what pyschools really want me to do or where am going wrong?
Thanks!
You’re using Python 3.x and they’re using Python 2.x. In Python 2.x, the
/operation is always an integer division when the arguments are integers.1/2is0. So, usefloat()to change one of your arguments to a floating-point number, such asint((float(a) / b) * 100). Thena/bwill have a fractional part.Or, assuming they are using a recent version of Python 2.x, you can just add this to the beginning of your script and it should work on the site:
As an aside, why are you assigning your input parameters to variables? They’re already variables. If you want them named
aandb, just receive them that way: