views:

167

answers:

4

I am trying to do integration in Python but whenever I key in a value my outputs always results in 0. What the reason?

E.g.:

def main():
   eq_of_form()

   value_of_a()
   value_of_b()
   value_of_c()
   value_of_m()
   value_of_n()

   value_of_x()
   area_under_graph()

def eq_of_form():
   print "Eq of the form y = ax^m + bx^n + c " + ":"

def value_of_a():
   return raw_input("Enter value for a \n")

def value_of_b():
   return raw_input("Enter value for b \n")

def value_of_c():
   return raw_input("Enter value for c \n")

def value_of_m():
   return raw_input("Enter value for m \n")

def value_of_n():
   return raw_input("Enter value for n \n")

def value_of_x():
   return raw_input("Enter a value for x to find " +
                 "value of y and the gradient at that point \n " + "x =   ")

def area_under_graph():
   y = (int(value_of_a())*int(value_of_x())**(int(value_of_m())+1))/((int(value_of_m())+1))
   // * 2nd part.This works for me(: 
   // + (int(value_of_b())) * (int(value_of_x())**
   // (int(value_of_n())+1))/(int(value_of_n())+1) + ((int(value_of_c())*int(value_of_x())))

   print y

main()

(* Note: the eq under the area_under_graph() function is only half of it because the other half kind of work so I did not post it:)) For the top code, I tried inputting the values here: (maybe you can try using the same(: )

a = 1
b = 2
c = 1
m = 2
n = 1
x = 1

I am supposed to get 7/3 which is 2.333, but I end up getting 2. The problem appears to lie in the first part of the eq.

Sorry for the newbie question.

+2  A: 

Your code at the start is wrong. You need to assign your variables after you read the user input:

value_of_a()

should be:

a = value_of_a()

It is also unnecessary to write a separate function for inputting each variable. You could instead have a function that takes a parameter:

def get_user_value(name):
    return raw_input("Enter value for %s\n" % name)

a = get_user_value("a")
b = get_user_value("b")
# etc..

But then you ignore all these values and read them again inside the area_under_curve() method. This is probably not what you intend to do. Furthermore inside this method you assume that all parameters are integers. If you are using Python 2.5 the division here is integer division:

m1/m2

This could return 0 when the result was actually supposed to be a non-integer like 0.125. You need to use floats instead of integers to do the calculation. You can do this in Python 2.5 using float(m). In Python 3.0 the division operator does what you want by default.

Mark Byers
Thanks to all! i got it already!
blur959
+3  A: 

/ does Integer division in Python2, this means a/b is the biggest integer c with c*b <=a, so 7/3 is indeed 2. You want floats, so you need to use them .. replace all the int with float in your code.

You should probably take another look at functions too ... you code can be much much shorter :-)

THC4k
Note that implicit int division is generally considered to have been a mistake and it changes in Python 3. See http://www.python.org/dev/peps/pep-0238/
bobince
A: 

In Python 2.x, dividing an integer by another integer results in an integer. Either use from __future__ import division, or turn one of the integers into a float by passing it to float().

Ignacio Vazquez-Abrams
Turning one of them at random is not always enough. `float(5)+6/7` for example returns 5.0. To know which one to change to a float you have to understand precendence rules. I suspect that the poster doesn't understand this and therefore better advice for him is to change *all of them* to floats.
Mark Byers
A: 

The issue is that you're using integer arithmetic - see all those int calls you've got everywhere. Integer arithmetic (in Python 2.x) will only ever return integers, so you'll never get 2.33, only 2.

Use float() instead of int() and things should work.

Daniel Roseman