Need help (Basic python)...what did I do wrong?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • frankie_85

    #1

    Need help (Basic python)...what did I do wrong?

    Hi everyone,

    I just made a simple code which is part of my assignment but I can't
    figure it out what's wrong with it. (always give me error messages)

    What the code basically does is:

    a function that takes one floating point number x as its argument and
    returns (the square root of the absolute value of x) plus (5 times x
    cubed).

    and read 5 floating point values from the user into a list then apply
    the function to each value in the list and print the results in reverse
    order.

    --------------------------------------------------------------------------------------------
    import math

    print "Enter 5 values:"
    a = float(raw_input ())
    b = float(raw_input ())
    c = float(raw_input ())
    d = float(raw_input ())
    e = float(raw_input ())

    def funct(a, b, c, d, e):
    a_1 = math.sqrt(math. fabs(a)) + 5((math.pow(a,3 )))
    b_2 = math.sqrt(math. fabs(b)) + 5((math.pow(b,3 )))
    c_3 = math.sqrt(math. fabs(c)) + 5((math.pow(c,3 )))
    d_4 = math.sqrt(math. fabs(d)) + 5((math.pow(d,3 )))
    e_5 = math.sqrt(math. fabs(e)) + 5((math.pow(e,3 )))
    return a_1
    return b_2
    return c_3
    return d_4
    return e_5
    print e_5, d_4, c_3, b_2, a_1



    funct(a, b, c, d, e)


    --------------------------------------------------------------------------------------------

    it always gives me these error messages:

    Traceback (most recent call last):
    File "/Users/x/Documents/test3.py", line 25, in <module>
    funct(a, b, c, d, e)
    File "/Users/x/Documents/test3.py", line 11, in funct
    a_1 = math.sqrt(math. fabs(a)) + 5((math.pow(a,3 )))
    TypeError: 'int' object is not callable

    What did I do wrong? Please help me

    Thanks in advance

  • mensanator@aol.com

    #2
    Re: Need help (Basic python)...what did I do wrong?


    frankie_85 wrote:
    Hi everyone,
    >
    I just made a simple code which is part of my assignment but I can't
    figure it out what's wrong with it. (always give me error messages)
    >
    What the code basically does is:
    >
    a function that takes one floating point number x as its argument and
    returns (the square root of the absolute value of x) plus (5 times x
    cubed).
    >
    and read 5 floating point values from the user into a list then apply
    the function to each value in the list and print the results in reverse
    order.
    >
    --------------------------------------------------------------------------------------------
    import math
    >
    print "Enter 5 values:"
    a = float(raw_input ())
    b = float(raw_input ())
    c = float(raw_input ())
    d = float(raw_input ())
    e = float(raw_input ())
    >
    def funct(a, b, c, d, e):
    a_1 = math.sqrt(math. fabs(a)) + 5((math.pow(a,3 )))
    b_2 = math.sqrt(math. fabs(b)) + 5((math.pow(b,3 )))
    c_3 = math.sqrt(math. fabs(c)) + 5((math.pow(c,3 )))
    d_4 = math.sqrt(math. fabs(d)) + 5((math.pow(d,3 )))
    e_5 = math.sqrt(math. fabs(e)) + 5((math.pow(e,3 )))
    return a_1
    return b_2
    return c_3
    return d_4
    return e_5
    print e_5, d_4, c_3, b_2, a_1
    >
    >
    >
    funct(a, b, c, d, e)
    >
    >
    --------------------------------------------------------------------------------------------
    >
    it always gives me these error messages:
    >
    Traceback (most recent call last):
    File "/Users/x/Documents/test3.py", line 25, in <module>
    funct(a, b, c, d, e)
    File "/Users/x/Documents/test3.py", line 11, in funct
    a_1 = math.sqrt(math. fabs(a)) + 5((math.pow(a,3 )))
    TypeError: 'int' object is not callable
    >
    What did I do wrong?
    You forgot the *.
    Please help me
    >
    Thanks in advance

    Comment

    • Luuk

      #3
      Re: Need help (Basic python)...what did I do wrong?


      "frankie_85 " <steveadi@gmail .comschreef in bericht
      news:1162024619 .913197.302700@ m73g2000cwd.goo glegroups.com.. .
      Hi everyone,
      >
      I just made a simple code which is part of my assignment but I can't
      figure it out what's wrong with it. (always give me error messages)
      >
      What the code basically does is:
      >
      a function that takes one floating point number x as its argument and
      returns (the square root of the absolute value of x) plus (5 times x
      cubed).
      >
      and read 5 floating point values from the user into a list then apply
      the function to each value in the list and print the results in reverse
      order.
      >
      --------------------------------------------------------------------------------------------
      import math
      >
      print "Enter 5 values:"
      a = float(raw_input ())
      b = float(raw_input ())
      c = float(raw_input ())
      d = float(raw_input ())
      e = float(raw_input ())
      >
      def funct(a, b, c, d, e):
      a_1 = math.sqrt(math. fabs(a)) + 5((math.pow(a,3 )))
      b_2 = math.sqrt(math. fabs(b)) + 5((math.pow(b,3 )))
      c_3 = math.sqrt(math. fabs(c)) + 5((math.pow(c,3 )))
      d_4 = math.sqrt(math. fabs(d)) + 5((math.pow(d,3 )))
      e_5 = math.sqrt(math. fabs(e)) + 5((math.pow(e,3 )))
      return a_1
      return b_2
      return c_3
      return d_4
      return e_5
      print e_5, d_4, c_3, b_2, a_1
      >
      >
      >
      funct(a, b, c, d, e)
      >
      >
      --------------------------------------------------------------------------------------------
      >
      it always gives me these error messages:
      >
      Traceback (most recent call last):
      File "/Users/x/Documents/test3.py", line 25, in <module>
      funct(a, b, c, d, e)
      File "/Users/x/Documents/test3.py", line 11, in funct
      a_1 = math.sqrt(math. fabs(a)) + 5((math.pow(a,3 )))
      TypeError: 'int' object is not callable
      >
      What did I do wrong? Please help me
      >
      Thanks in advance
      >

      obviously the error is in the line "a_1 = math.sqrt(math. fabs(a)) +
      5((math.pow(a,3 )))"

      try changin this to "a_1 = math.sqrt(math. fabs(a)) + 5*math.pow(a,3) "

      because you seem to want 5 time the result of the pow() function




      Comment

      • Fredrik Lundh

        #4
        Re: Need help (Basic python)...what did I do wrong?

        frankie_85 wrote:
        a_1 = math.sqrt(math. fabs(a)) + 5((math.pow(a,3 )))
        TypeError: 'int' object is not callable
        hint: in most programming languages, you have to use an explicit
        operator to spell out multiplication. Python's no exception.

        </F>

        Comment

        • Ben Finney

          #5
          Re: Need help (Basic python)...what did I do wrong?

          "frankie_85 " <steveadi@gmail .comwrites:
          I just made a simple code which is part of my assignment
          You may want to review the restrictions your educational institution
          has on collusion.
          a function that takes one floating point number x as its argument
          and returns (the square root of the absolute value of x) plus (5
          times x cubed).
          >
          and read 5 floating point values from the user into a list then
          apply the function to each value in the list and print the results
          in reverse order.
          >
          --------------------------------------------------------------------------------------------
          import math
          >
          print "Enter 5 values:"
          a = float(raw_input ())
          b = float(raw_input ())
          c = float(raw_input ())
          d = float(raw_input ())
          e = float(raw_input ())
          This reads five values, but assigns five separate names to them. You
          will want to review your course notes on Python to find out what a
          'list' is.
          it always gives me these error messages:
          >
          Traceback (most recent call last):
          File "/Users/x/Documents/test3.py", line 25, in <module>
          funct(a, b, c, d, e)
          File "/Users/x/Documents/test3.py", line 11, in funct
          a_1 = math.sqrt(math. fabs(a)) + 5((math.pow(a,3 )))
          TypeError: 'int' object is not callable
          >
          What did I do wrong? Please help me
          The error message is telling you that you have "called" an integer
          ('int') object. Integer objects don't support being called like
          functions.

          The syntax for "calling" an object is:

          foo(something, something_else)

          Look at the line of code reported in the error, and you'll find an
          integer object being called in that fashion. You'll need to rewrite it
          so it does what you're actually trying to do.

          Again, please make sure you work on these problems yourself; your
          assessment should not be testing your ability to ask on the internet
          for assistance.

          --
          \ "All good things are cheap; all bad are very dear." -- Henry |
          `\ David Thoreau |
          _o__) |
          Ben Finney

          Comment

          • Steven D'Aprano

            #6
            Re: Need help (Basic python)...what did I do wrong?

            On Sat, 28 Oct 2006 18:56:33 +1000, Ben Finney wrote:
            "frankie_85 " <steveadi@gmail .comwrites:
            >
            >I just made a simple code which is part of my assignment
            >
            You may want to review the restrictions your educational institution
            has on collusion.
            [snip]
            Again, please make sure you work on these problems yourself; your
            assessment should not be testing your ability to ask on the internet
            for assistance.
            I don't think the restrictions against collusion are meant to prohibit
            simple "what does this error message mean?" type questions. It would be
            a funny sort of learning process that forced students to live in a
            vacuum, never discussing their topic with anyone else, never asking the
            most trivial questions.


            --
            Steven.

            Comment

            • James Stroud

              #7
              Re: Need help (Basic python)...what did I do wrong?

              Steven D'Aprano wrote:
              On Sat, 28 Oct 2006 18:56:33 +1000, Ben Finney wrote:
              >
              >"frankie_85 " <steveadi@gmail .comwrites:
              >>
              >>I just made a simple code which is part of my assignment
              >You may want to review the restrictions your educational institution
              >has on collusion.
              [snip]
              >Again, please make sure you work on these problems yourself; your
              >assessment should not be testing your ability to ask on the internet
              >for assistance.
              >
              I don't think the restrictions against collusion are meant to prohibit
              simple "what does this error message mean?" type questions. It would be
              a funny sort of learning process that forced students to live in a
              vacuum, never discussing their topic with anyone else, never asking the
              most trivial questions.
              >
              >
              I think you are wrong. The new trend is toward absolutely no instruction
              at all and the students must guess the assignment. In this regard, I
              believe the OP is doing very well. Don't you think?

              James

              Comment

              • Antoine De Groote

                #8
                Re: Need help (Basic python)...what did I do wrong?

                frankie_85 wrote:
                Hi everyone,
                >
                I just made a simple code which is part of my assignment but I can't
                figure it out what's wrong with it. (always give me error messages)
                >
                What the code basically does is:
                >
                a function that takes one floating point number x as its argument and
                returns (the square root of the absolute value of x) plus (5 times x
                cubed).
                >
                You also may want to review what (the) return (keyword) really means.
                Hopefully this will point you to the other problem in your code. ;-)

                Comment

                • Fredrik Lundh

                  #9
                  Re: Need help (Basic python)...what did I do wrong?

                  Steven D'Aprano wrote:
                  I don't think the restrictions against collusion are meant to prohibit
                  simple "what does this error message mean?" type questions. It would be
                  a funny sort of learning process that forced students to live in a
                  vacuum, never discussing their topic with anyone else, never asking the
                  most trivial questions.
                  the python-tutor policy is quite reasonable:



                  </F>

                  Comment

                  • frankie_85

                    #10
                    Re: Need help (Basic python)...what did I do wrong?

                    Thanks for some of the helps guys.

                    Hmmm....after more trials and errors, I think what I did wrong is along
                    on these lines:

                    a_1 = math.sqrt(math. fabs(a)) + 5((math.pow(a,3 )))
                    b_2 = math.sqrt(math. fabs(b)) + 5((math.pow(b,3 )))
                    c_3 = math.sqrt(math. fabs(c)) + 5((math.pow(c,3 )))
                    d_4 = math.sqrt(math. fabs(d)) + 5((math.pow(d,3 )))
                    e_5 = math.sqrt(math. fabs(e)) + 5((math.pow(e,3 )))

                    but I still don't understand though why the variable a, b, c, d, e
                    becomes an int type even though I have already specified the user input
                    to be floating point?

                    Once again thanks for all your help

                    Comment

                    • Paul Rubin

                      #11
                      Re: Need help (Basic python)...what did I do wrong?

                      "frankie_85 " <steveadi@gmail .comwrites:
                      e_5 = math.sqrt(math. fabs(e)) + 5((math.pow(e,3 )))
                      >
                      but I still don't understand though why the variable a, b, c, d, e
                      becomes an int type even though I have already specified the user input
                      to be floating point?
                      if you want to multiply two numbers you need to put a * between them.
                      * is the multiplication symbol in Python and most other languages.

                      When you say f(x) that says "there is a function x and Python should
                      call it on the value x". When you say 5(x), that says "there is
                      a function 5 and Python should call it with the value x". And then
                      when you try to actually do that, Python says "hey I can't do that,
                      5 isn't a function, it's an integer".

                      Comment

                      • Fredrik Lundh

                        #12
                        Re: Need help (Basic python)...what did I do wrong?

                        frankie_85 wrote:
                        Hmmm....after more trials and errors, I think what I did wrong is along
                        on these lines:
                        >
                        a_1 = math.sqrt(math. fabs(a)) + 5((math.pow(a,3 )))
                        b_2 = math.sqrt(math. fabs(b)) + 5((math.pow(b,3 )))
                        c_3 = math.sqrt(math. fabs(c)) + 5((math.pow(c,3 )))
                        d_4 = math.sqrt(math. fabs(d)) + 5((math.pow(d,3 )))
                        e_5 = math.sqrt(math. fabs(e)) + 5((math.pow(e,3 )))
                        >
                        but I still don't understand though why the variable a, b, c, d, e
                        becomes an int type even though I have already specified the user input
                        to be floating point?
                        hint: besides the variables, what other values are involved in your
                        calculations? do you see any integers among them?

                        </F>

                        Comment

                        Working...