pythonpython-2.7

Python Not Outputting Correct Values


Here's the syntax of the problem I'm facing:

Heating and cooling degree-days are measured by utility companies to estimate energy requirements. If the average temperature for a day is below 60, then the number of degrees below 60 is added to the heating degree-days. If the temperature is above 80, the amount over 80 is added to the cooling degree-days. Write a program that accepts a sequence of average daily temps and computes the running total of cooling and heating degree-days. The program should print these two totals after all the data has been processed.

When I run my program, it will let me input temps, but when I press enter to signify I'm done entering in data I get the return "Unknown error". Thanks for the assistance.

def main():
print("Please enter daily average temperature below. Leave empty when finish.")

hdd,cdd,hot,cool = 0,0,0,0
date = 1
try:
    temp = input("Day #{} :".format(date))

    while temp != "":
        temp = int(temp)

        if temp > 80:
            cdd = (temp-80)+cdd
        if temp < 60:
            hdd = (60-temp)+hdd

        date = date+1
        temp = input("Day #{} :".format(date))

    print("In {} days, there\'r total of {} HDD and {} CDD.".format(date-1,hdd,cdd))

except ValueError:
    print('Please correct your data.')
except:
    print('Unknown error.')

main()

Solution

  • Use raw_input() instead of input(). Your temp variable is attemping to 'be' an int when it's null (because it's "").

    It's giving you a syntax error because input() attempts to evaluate the expression you put in. You should stick with raw_input() and cast the value to whatever you need it to be until you know you actually need input() for something specific.

    After changing both input()s to raw_input():

    Day #1 :1
    Day #2 :2
    Day #3 :3
    Day #4 :90
    Day #5 :90
    Day #6 :
    6 174 20
    In 5 days, there'r total of 174 HDD and 20 CDD.