0

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()

2 Answers2

0

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.
Mdev
  • 2,440
  • 2
  • 17
  • 25
0

The error is because of your usage of input() on python 2.7. It is hitting this error:

SyntaxError: unexpected EOF while parsing

which your program will not display thanks to your last except clause.

The cause of the error is because input() on python 2.7 is equivalent to getting an input and executing it. In your case it is trying to execute no input.

Use raw_input() and your code will run fine.

More details on the error with input() on python 2.7 is discussed here - Why input() gives an error when I just press enter?

Community
  • 1
  • 1
shaktimaan
  • 11,962
  • 2
  • 29
  • 33