Results 1 to 10 of 10

Thread: python return functions incorrect

  1. #1
    Join Date
    Jan 2012
    Beans
    161

    python return functions incorrect

    Hi people, this is some python code I have been working on...

    def add(a, b):
    return a + b

    def divide(a, b):
    return a / b

    def subtract(a, b):
    return a - b

    Code:
    print " I will attempt to get 995.6 from computational math.  I hope it works."
    
    print "Give me the first number."
    
    var1 = float(raw_input(">"))
    
    print "Give me number 2."
    
    var2 = float(raw_input(">"))
    
    print "Ok, number 3."
    
    var3 = float(raw_input(">"))
    
    print "Ok, the fourth number."
    
    var4 = float(raw_input(">"))
    
    formula1 = divide(var2, var3)
    
    formula2 = subtract(formula1, var4)
    
    print add(var1, formula2)
    these are the values I provide:

    var1=24
    var2=34
    var3=100
    var4=1023

    i get -998.66 from the program, but the math says the answer should be -995.6

    Why is that?

  2. #2
    Join Date
    Jan 2012
    Beans
    161

    Re: python return functions incorrect

    whoops! Those function definitions are part of the code too

  3. #3
    Join Date
    Nov 2005
    Location
    Bordeaux, France
    Beans
    11,292
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: python return functions incorrect

    Quote Originally Posted by wingnut2626 View Post
    i get -998.66 from the program, but the math says the answer should be -995.6
    Your math is wrong, Python is right.
    「明後日の夕方には帰ってるからね。」


  4. #4
    Join Date
    Jul 2012
    Location
    Elmira, NY
    Beans
    265
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: python return functions incorrect

    Code:
    metulburr@ubuntu:~$ python3
    Python 3.2.3 (default, Oct 19 2012, 20:10:41) 
    [GCC 4.6.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 34 / 100
    0.34
    >>> 0.34 - 1023
    -1022.66
    >>> 24 + -1022.66
    -998.66
    >>>
    Code:
    metulburr@ubuntu:~$ python
    Python 2.7.3 (default, Aug  1 2012, 05:14:39) 
    [GCC 4.6.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 34 / 100
    0
    >>> 0 - 1023
    -1023
    >>> 24 + -1023
    -999
    >>>

  5. #5
    Join Date
    Nov 2005
    Location
    Bordeaux, France
    Beans
    11,292
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: python return functions incorrect

    What are you trying to say here?

    Quote Originally Posted by micahpage View Post
    Code:
    metulburr@ubuntu:~$ python3
    Python 3.2.3 (default, Oct 19 2012, 20:10:41) 
    [GCC 4.6.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 34 / 100
    0.34
    >>> 0.34 - 1023
    -1022.66
    >>> 24 + -1022.66
    -998.66
    >>>
    Code:
    metulburr@ubuntu:~$ python
    Python 2.7.3 (default, Aug  1 2012, 05:14:39) 
    [GCC 4.6.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 34 / 100
    0
    >>> 0 - 1023
    -1023
    >>> 24 + -1023
    -999
    >>>
    「明後日の夕方には帰ってるからね。」


  6. #6
    Join Date
    Jul 2012
    Location
    Elmira, NY
    Beans
    265
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: python return functions incorrect

    was just elaborating more on:
    Your math is wrong, Python is right.

  7. #7
    Join Date
    Nov 2005
    Location
    Bordeaux, France
    Beans
    11,292
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: python return functions incorrect

    Quote Originally Posted by micahpage View Post
    was just elaborating more on:
    And why the two Python outputs (one of which is incorrect)?
    「明後日の夕方には帰ってるからね。」


  8. #8
    Join Date
    Jul 2012
    Location
    Elmira, NY
    Beans
    265
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: python return functions incorrect

    And why the two Python outputs (one of which is incorrect)?
    wasnt paying attention to the fact he float'd the input values

  9. #9
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: python return functions incorrect

    i see two different python versions, does python3 really drop to float during integer division?
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  10. #10
    Join Date
    Jul 2012
    Location
    Elmira, NY
    Beans
    265
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: python return functions incorrect

    i see two different python versions, does python3 really drop to float during integer division?
    in 3.x the / operator does the floating-point division, and the // operator to do truncating division
    Last edited by micahpage; February 5th, 2013 at 07:13 PM.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •