【发布时间】:2020-12-09 05:08:55
【问题描述】:
我非常接近解决这个问题,但我必须正视解决方案。该程序旨在接收以摄氏度为单位的温度值,将其转换为华氏度,然后计算“凉爽”、“温暖”和“炎热”天数。我已经弄清楚了这个问题的前两部分,但无论出于何种原因,我的程序都没有正确计算每种日子的数量。
temperature_list = []
value = int(input("Number of temperatures to enter: "))
for i in range(value):
#We now collect the values
t = int(input("Enter temperature: "))
temperature_list.append(t)
print("Your entered temperatures in Celsius are: ", temperature_list)
#Next up is to print those temperatures in Fahrenheit, which we'll do in a batch
f_list = list()
f = [t*1.8 + 32 for t in temperature_list]
f_list.append(f)
fint = int(f[0])
cooldays = 0
hotdays = 0
for f in f_list:
if fint > 65 :
cooldays = cooldays + 1
if fint < 80 :
hotdays = hotdays + 1
print("Your temperature in Fahrenheit is: ", f[:])
warmdays = (len(f_list) - (cooldays + hotdays))
print(cooldays)
print(hotdays)
print(warmdays)
有人能告诉我我错过了什么吗?
【问题讨论】:
-
当您可能应该使用
int(f)时,您在循环中使用fint作为条件句 -
您的逻辑还允许
cooldays和hotdays在同一天发生,从而使您的暖日无效 -
我认为您的 > 和
-
您提到的计数不正确。你能告诉我们当前的输出和预期的输出吗?不要忘记告诉我们输入
-
Grand J,你说得对。我经常重做线条,以至于我忽略了检查标志的朝向!不过,用“f”替换“fint”给了我通常的“'