1

Okay So I barely started class a few days ago and everything is new to me. What I am suppose to be doing is creating something that will convert Celsius temps into Fahrenheit and also Kelvin, I've done that although it is a bit sloppy.

Anyways my issue is with the accumulator. when i total up the sum of all the Celsius degrees i get 231 when it should be 210. It added the 21 i had from "While celsius < 21.

Can anyone help me figure out how to get the correct number :/? Thanks!

print ("Celsius Conversion Chart")


sum = 0
sum1 = 0
sum2 = 0
celsius = 0

while celsius < 21:
    print (str(celsius)+'°C ' + ' = ' + str(celsius*9/5+32)+'°F ' + ' = ' + str(celsius +273.15)+'°K')
    celsius += 1
    sum = sum + celsius
    sum1 = sum1 + (celsius * 9/5 + 32)
    sum2= sum2 + (celsius + 273.15)

print (str(sum1)+'°F')
print (str(sum2)+'°K')
print (str(sum)+'°C')

if celsius < 100:
    print ("WARNING: temperature is too high")
Shorsh
  • 21
  • 2
  • possible duplicate of [how can I force division to be floating point in Python?](http://stackoverflow.com/questions/1267869/how-can-i-force-division-to-be-floating-point-in-python) – chepner Jul 07 '14 at 18:54
  • 3
    try moving `celsius += 1` to the end of the loop. Or maybe even do `for celsius in range(21):` instead of `while` – Kevin Jul 07 '14 at 18:55
  • 3
    @chepner: That question has nothing to do with this one. – jwodder Jul 07 '14 at 18:56
  • Kevin the celsius +=1 at the end of the loop worked perfectly! Thank you I was stuck on that for a few hours :|. – Shorsh Jul 07 '14 at 19:00
  • 1
    Adding together temperatures like this doesn't really make physical sense; a change in temperature is different from a temperature, in much the same way that a length of time is different from a point in time. – Karl Knechtel Jul 08 '14 at 05:34

3 Answers3

5

The problem is that the entry condition of the while loop is celsius < 21. So when celsius is 20, the while loop will be entered, celsius will be incremented to 21, your sum values will be changed and now the loop is done, because celsius < 21 is False. One solution is to simply check for celsius < 20.

Demo of what is happening:

# python interpreter
>>> x = 0
>>> while x < 3:
...    x += 1
...    print(x)
... 
1
2
3
timgeb
  • 76,762
  • 20
  • 123
  • 145
  • hey Tim thanks for your response! The reason I have it set to 21 currently is because when i set it to celsius < 20 it only shows 1-19 and im not sure how to make it include the number 20 without making the line read celsius < 21. – Shorsh Jul 07 '14 at 19:01
  • In that case, go with @mauve's answer – timgeb Jul 07 '14 at 19:03
3

Move your celsius += 1 line to below the sum updates. That will fix that particular problem.

Your warning should be if celsius > 100, not celsius < 100.

Also, you should probably use sum1, sum2, and sum3, since "sum" is a reserved word.

mauve
  • 2,707
  • 1
  • 20
  • 34
  • Thank you! I was playing around with it so much I must have changed that and forgot to put it back to what it should read! Thank you for catching that for me!. – Shorsh Jul 07 '14 at 19:04
  • Also, not to be pedantic (but this is) - Kelvin normally isn't represented with a degree sign. – mauve Jul 07 '14 at 19:08
  • oh! Thanks again. you two are on a roll! I greatly appreciate your input! I wasn't aware of that :). – Shorsh Jul 07 '14 at 19:11
0

I can't comment yet so posted as an answer...

Kevin mentioned moving the counter. I add that in while loops, the counter inside the loop should always be after all processing that uses the counter. This applies in any language that supports while loops, i.e. it's how while loops are done and not specific to python.

So it's not a bad entry condition. Essentially, setting the initial value of Celsius = 0 is the first count. So inside the loop, you have to do your processing and then increment the counter.

ScottO
  • 129
  • 4