Conversion code not working properly

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

    Conversion code not working properly

    Why is the Farenheit to Celsius part not working properly? Instead of
    showing a similar range of what the farenheit is listing, the celsius
    portion is showing half the range of farenheit.

    print "\nWelcome to the Temperature program"

    while True:
    low1 = raw_input("Plea se enter a low temperature: ").upper()
    if low1 != 'I QUIT':
    low = int(low1)
    if low1 == 'I QUIT':
    break


    high = int(raw_input(" Please enter a high temperature: "))
    temp = raw_input('Plea se indicate Celsius or Fahrenheit ').upper()

    while not temp or temp not in 'CF':
    temp = raw_input('Plea se indicate Celsius or Fahrenheit
    ').upper()

    if temp == 'C' and low <= high:

    low_other_temp = (9 / 5 * low) + 32
    high_other_temp = (9 / 5 * high) + 32
    rnge = range(low, high + 1)
    faren = range(low_other _temp, high_other_temp + 1)
    print rnge
    print faren

    elif temp == 'F' and low <= high:

    num1 = low - 32
    num2 = high - 32
    low_temp = num1 * 5/9
    high_temp = num2 * 5/9
    rnge = range(low, high + 1)
    celsi = range(low_temp, high_temp + 1)
    print rnge
    print celsi

    else:
    print 'Invalid temperatures specified! The low must be less
    than the high!'

    exit = raw_input('Hit <enter> to continue. Type " I quit" in the
    low temp field to exit.')
    print 'Returning to main.'
  • Andrei

    #2
    Re: Conversion code not working properly

    weasel wrote on Sun, 22 Feb 2004 19:07:01 GMT:
    [color=blue]
    > Why is the Farenheit to Celsius part not working properly? Instead of
    > showing a similar range of what the farenheit is listing, the celsius
    > portion is showing half the range of farenheit.[/color]

    Try this in your interactive interpreter:
    [color=blue][color=green][color=darkred]
    >>> 9/5[/color][/color][/color]

    What's odd about the result? "/" does integer division. It's weird and
    unexpected to lots of people, but it's the way it is - it will be fixed in
    due time. However, due to some time machine Guido is said to possess, you
    can already use the future behavior by doing this:

    from __future__ import division

    Then try 9/5 again. Integer division is "//" now, while "/" behaves as
    floating-point division.

    Alternatively, you could use a floating-point number instead of integers.
    Try this in a clean interpreter session (without the from __future__
    thing):
    [color=blue][color=green][color=darkred]
    >>> 9.0/5
    >>> 9/5.0
    >>> 9.0/5.0[/color][/color][/color]

    <snip>[color=blue]
    > low_other_temp = (9 / 5 * low) + 32
    > high_other_temp = (9 / 5 * high) + 32
    > rnge = range(low, high + 1)
    > faren = range(low_other _temp, high_other_temp + 1)
    > print rnge
    > print faren
    >
    > elif temp == 'F' and low <= high:
    >
    > num1 = low - 32
    > num2 = high - 32
    > low_temp = num1 * 5/9
    > high_temp = num2 * 5/9
    > rnge = range(low, high + 1)
    > celsi = range(low_temp, high_temp + 1)
    > print rnge
    > print celsi[/color]
    <snip>

    --
    Yours,

    Andrei

    =====
    Real contact info (decode with rot13):
    cebwrpg5@jnanqb b.ay. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
    gur yvfg, fb gurer'f ab arrq gb PP.

    Comment

    • weasel

      #3
      Re: Conversion code not working properly

      It's not working. I don't think the conversion is the problem. After
      converting, the program shows the original entry you put in

      i.e. Please enter low temp:
      32
      Please enter high temp:
      42
      Please enter a conversion letter (C, F):
      F

      Then, the display shows
      32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42

      However,

      the conversion is only showing
      0, 1, 2, 3, 4, 5.

      What happened to the other half? I tried using 5.0/9.0, but got the
      same results.

      Any more ideas?


      On Sun, 22 Feb 2004 20:32:50 +0100, Andrei <fake@fake.ne t> wrote:
      [color=blue]
      >weasel wrote on Sun, 22 Feb 2004 19:07:01 GMT:
      >[color=green]
      >> Why is the Farenheit to Celsius part not working properly? Instead of
      >> showing a similar range of what the farenheit is listing, the celsius
      >> portion is showing half the range of farenheit.[/color]
      >
      >Try this in your interactive interpreter:
      >[color=green][color=darkred]
      >>>> 9/5[/color][/color]
      >
      >What's odd about the result? "/" does integer division. It's weird and
      >unexpected to lots of people, but it's the way it is - it will be fixed in
      >due time. However, due to some time machine Guido is said to possess, you
      >can already use the future behavior by doing this:
      >
      >from __future__ import division
      >
      >Then try 9/5 again. Integer division is "//" now, while "/" behaves as
      >floating-point division.
      >
      >Alternativel y, you could use a floating-point number instead of integers.
      >Try this in a clean interpreter session (without the from __future__
      >thing):
      >[color=green][color=darkred]
      >>>> 9.0/5
      >>>> 9/5.0
      >>>> 9.0/5.0[/color][/color]
      >
      ><snip>[color=green]
      >> low_other_temp = (9 / 5 * low) + 32
      >> high_other_temp = (9 / 5 * high) + 32
      >> rnge = range(low, high + 1)
      >> faren = range(low_other _temp, high_other_temp + 1)
      >> print rnge
      >> print faren
      >>
      >> elif temp == 'F' and low <= high:
      >>
      >> num1 = low - 32
      >> num2 = high - 32
      >> low_temp = num1 * 5/9
      >> high_temp = num2 * 5/9
      >> rnge = range(low, high + 1)
      >> celsi = range(low_temp, high_temp + 1)
      >> print rnge
      >> print celsi[/color]
      ><snip>[/color]

      Comment

      • Terry Reedy

        #4
        Re: Conversion code not working properly


        "weasel" <healthed@hotma il.com> wrote in message
        news:3uuh305tv9 o3g5dpenoqaghtb kiuihdpua@4ax.c om...[color=blue]
        > Why is the Farenheit to Celsius part not working properly?[/color]

        Specific examples of what you put in, what you get out, and what you
        expected out and why often helps get answers.
        [color=blue]
        > Instead of
        > showing a similar range of what the farenheit is listing, the celsius
        > portion is showing half the range of farenheit.[/color]

        Celcius ranges are 5/9ths, approximately 1/2, the corresponding Farenheit
        ranges.

        Terry J. Reedy




        Comment

        • weasel

          #5
          Re: Conversion code not working properly

          see my other post. I've got an example of what's going wrong.

          On Sun, 22 Feb 2004 14:27:28 -0500, "Terry Reedy" <tjreedy@udel.e du>
          wrote:
          [color=blue]
          >
          >"weasel" <healthed@hotma il.com> wrote in message
          >news:3uuh305tv 9o3g5dpenoqaght bkiuihdpua@4ax. com...[color=green]
          >> Why is the Farenheit to Celsius part not working properly?[/color]
          >
          >Specific examples of what you put in, what you get out, and what you
          >expected out and why often helps get answers.
          >[color=green]
          >> Instead of
          >> showing a similar range of what the farenheit is listing, the celsius
          >> portion is showing half the range of farenheit.[/color]
          >
          >Celcius ranges are 5/9ths, approximately 1/2, the corresponding Farenheit
          >ranges.
          >
          >Terry J. Reedy
          >
          >
          >[/color]

          Comment

          • weasel

            #6
            Re: Conversion code not working properly

            I just figured it out. It has to do with range. The conversion is
            working, but the range functionality is screwing it all up. Back to
            the drawing board!

            Comment

            • Andrei

              #7
              Re: Conversion code not working properly

              weasel wrote on Sun, 22 Feb 2004 19:45:30 GMT:
              [color=blue]
              > It's not working. I don't think the conversion is the problem. After
              > converting, the program shows the original entry you put in[/color]

              You're right, I saw integer division where you were trying to convert
              temperatures and automatically assumed that was the only problem, without
              really reading the code.
              [color=blue]
              > What happened to the other half? I tried using 5.0/9.0, but got the
              > same results.[/color]

              How did you get range() to work on floats without deprecation warnings?
              [color=blue]
              > Any more ideas?[/color]

              Terry Reedy answered a short while ago to why a Celsius range is by
              definition shorter than a Fahrenheit range. I'm not quite sure what the
              point of the conversion is to be honest, more specifically why a range is
              returned. You can't map a celsius temperature in the Celsius range to a
              Fahrenheid temperature in the other range and the extreme values are
              incorrect due to the integer division and/or the range() used with float
              arguments. 42 F should be 5.556 C and your program returns 5.

              Btw, there's also a Python list for beginners if you're interested
              (python-tutor).

              --
              Yours,

              Andrei

              =====
              Real contact info (decode with rot13):
              cebwrpg5@jnanqb b.ay. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
              gur yvfg, fb gurer'f ab arrq gb PP.

              Comment

              • weasel

                #8
                Re: Conversion code not working properly

                Well, I know that the range command is the problem, because it just
                keeps adding 1 which throws off the decimal entries. So, in removing
                range, I need to take the beginning temp, and add up to the ending
                temp.

                The conversion seems to be working fine, even by using "float" for the
                input.

                On Sun, 22 Feb 2004 21:15:30 +0100, Andrei <fake@fake.ne t> wrote:
                [color=blue]
                >weasel wrote on Sun, 22 Feb 2004 19:45:30 GMT:
                >[color=green]
                >> It's not working. I don't think the conversion is the problem. After
                >> converting, the program shows the original entry you put in[/color]
                >
                >You're right, I saw integer division where you were trying to convert
                >temperatures and automatically assumed that was the only problem, without
                >really reading the code.
                >[color=green]
                >> What happened to the other half? I tried using 5.0/9.0, but got the
                >> same results.[/color]
                >
                >How did you get range() to work on floats without deprecation warnings?
                >[color=green]
                >> Any more ideas?[/color]
                >
                >Terry Reedy answered a short while ago to why a Celsius range is by
                >definition shorter than a Fahrenheit range. I'm not quite sure what the
                >point of the conversion is to be honest, more specifically why a range is
                >returned. You can't map a celsius temperature in the Celsius range to a
                >Fahrenheid temperature in the other range and the extreme values are
                >incorrect due to the integer division and/or the range() used with float
                >arguments. 42 F should be 5.556 C and your program returns 5.
                >
                >Btw, there's also a Python list for beginners if you're interested
                >(python-tutor).[/color]

                Comment

                • weasel

                  #9
                  Re: Conversion code not working properly

                  On Sun, 22 Feb 2004 20:09:17 GMT, weasel <healthed@hotma il.com> wrote:
                  [color=blue]
                  >I just figured it out. It has to do with range. The conversion is
                  >working, but the range functionality is screwing it all up. Back to
                  >the drawing board![/color]


                  Just finished it. Thank you for the suggestions, it helped me move in
                  the right direction.

                  Comment

                  Working...