这是我面临的问题的语法:
公用事业公司对供热和制冷度-天数进行了估算,以估算能源需求。如果一天的平均温度低于60,则将低于60的度数加到加热天数。如果温度高于80,则将超过80的量加到冷却天数。编写一个程序,该程序接受一系列平均每日温度并计算制冷和制热日的运行总和。处理完所有数据后,程序应打印这两个总数。
当我运行程序时,它将允许我输入临时文件,但是当我按Enter表示输入完数据时,将得到返回“未知错误”。感谢您的协助。
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()
使用raw_input()
代替input()
。您的temp
变量尝试将其为null时设为“ int”(因为它为“”)。
这会给您带来语法错误,因为会input()
尝试评估您放入的表达式。您应该坚持使用raw_input()
该值并将其强制转换为所需的值,直到您知道实际需要input()
特定的内容为止。
将input()
s都更改为后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.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句