如何将Celcius转换为Farenheit,然后根据用户输入的温度计算寒冷和温暖的天数

2024-05-23 23:04:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经编写了一个程序,从用户那里获取摄氏度的输入,并将其转换为Farenheit。我把大部分节目都录下来了

我只是不知道如何让它输出寒冷天气和温暖天气的发生次数,这取决于用户输入

# This program will convert 10 entries from Celsius to Farenheit.
print('Lets convert the temperature from the past 10 day from celsius to farenheit')

# Create the open list and loop to prompt the user for all the temperatures in Farenheit.
temps = list()
celsius = int(input("Enter the temperature of everyday of the past 10 days in celsius: "))
while len(temps) != 10:
    temps.append(celsius)
    celsius = int(input("Enter the temperature of everyday of the past 10 days in celsius: "))
print("Okay, the temperature, in celsius of the past 10 days has been: ", temps)

# Using a for-loop, convert each entry by the user into Celsius and print the result.
for far in range(len(temps)):
    temps[far] = (temps[far] * 1.8) + 32
print("The temperatures for everyday of the past week, converted into farenheit, is: ", temps)

def cold():
    if temps[far] < 50:
        print(len(temps[far]))
if cold:
    print(len(temps))

def warm():
    if temps[far] is (>= 50 or < 85):
        print(len(temps[far]))
if warm:
    print(len(temps))

Tags: oftheinfromconvertforlenif
3条回答

在您的while之前有一行无用的代码,可以删除:

celsius = int(input("Enter the temperature of everyday of the past 10 days in celsius: "))

以下是您的代码版本,可满足您的要求:

def cold(temps):
    count=0
    for temperature in temps:
        if temperature <50:count+=1
    return count

def warm(temps):
    count=0
    for temperature in temps:
        if temperature >=50 and temperature<85:count+=1
    return count     

# This program will convert 10 entries from celsius to farenheit
print('Lets convert the temperature from the past 10 day from celsius to farenheit')
print("")

# Create the open list and loop to prompt the user for all the temperatures in farenheit
temps = list()
while len(temps) != 10:
    temps.append(celsius)
    celsius = int(input("Enter the temperature of everyday of the past 10 days in celsius: "))

print("")    
print("Okay, the temperature, in celsius of the past 10 days has been: ", temps)
print("")

# Using a for loop, convert each entry by the user into celsius and print the result
for far in range(len(temps)):
    temps[far] = (temps[far] * 1.8) + 32

print("")    
print ("The temperatures for everyday of the past week, converted into farenheit, is: ", temps)
print("")   

print("number of cold days",cold(temps))
print("number of warm days",warm(temps))

因此,您的代码中存在多个问题:

  1. 您使用的是范围变量,而不是将变量传递给函数
  2. 在while之前,您有一个额外的inputstatmenet,是什么导致了一个您没有做任何处理的额外输入
  3. 您在warm中的条件是错误的,没有temps[far] is (>= 50 or < 85):

因此,代码的工作版本是:

def cold(T):
    return T < 50

def warm(T):
    return T >= 50 and T <85

# This program will convert 10 entries from celsius to farenheit
print('Lets convert the temperature from the past 10 day from celsius to farenheit')
# Create the open list and loop to prompt the user for all the temperatures in farenheit
temps = list()
while len(temps) < 10:
    celsius = int(input("Enter the temperature of everyday of the past 10 days in celsius: "))
    temps.append(celsius)
print ("Okay, the temperature, in celsius of the past 10 days has been: ", temps)
# Using a for loop, convert each entry by the user into celsius and print the result
for far in range(len(temps)):
    temps[far] = (temps[far] * 1.8) + 32
print ("The temperatures for everyday of the past week, converted into farenheit, is: ", temps)

    
cold_days = sum(list(map(cold, temps)))
warm_days = sum(list(map(warm, temps)))
print("Cold days: {:}, Warm days: {:}".format(cold_days, warm_days))

首先,总是把函数去宽容放在主函数之上,这是一个很好的实践

其次,cold/warm将获得一个浮点值,并检查单个元素,而不是整个列表。我认为你的情况更好,但这要看情况而定

第三,通过使用map和返回值是布尔值(解释为0/1)这一事实,我们可以sum并获得所需的结果

最后,请阅读^{}。当您希望打印多个带有变量等的内容时,它可能很方便

由于您可能不熟悉map,这里有一个link来阅读

您声明了函数,现在必须在其中写入逻辑,这将帮助您计算温暖和寒冷天气的值。请看下面的代码

def cold(temps):
    counter = 0
    for far in range(len(temps)):
        if temps[far] < 50:
            counter += 1
    return counter

def warm(temps):
    counter = 0
    for far in range(len(temps)):
        if temps[far] >= 50 and temps[far] < 85:
            counter += 1
    return counter
    
# This program will convert 10 entries from celsius to farenheit
print('Lets convert the temperature from the past 10 day from celsius to farenheit')
# Create the open list and loop to prompt the user for all the temperatures in farenheit
temps = list()
celsius = int(input("Enter the temperature of everyday of the past 10 days in celsius: "))
while len(temps) != 10:
    temps.append(celsius)
    celsius = int(input("Enter the temperature of everyday of the past 10 days in celsius: "))
print ("Okay, the temperature, in celsius of the past 10 days has been: ", temps)
# Using a for loop, convert each entry by the user into celsius and print the result
for far in range(len(temps)):
    temps[far] = (temps[far] * 1.8) + 32
print ("The temperatures for everyday of the past week, converted into farenheit, is: ", temps)

print("Cold days: " + str(cold(temps)))
print("Warm days: " + str(warm(temps)))

编辑:

除了使用冷函数和热函数,您还可以使用lamba函数,如下所示:

print("Cold days: " + str(sum(value < 50 for value in temps)))
print("Warm days: " + str(sum(85 > value >= 50 for value in temps)))

而不是

print("Cold days: " + str(cold(temps)))
print("Warm days: " + str(warm(temps)))

现在您可以删除cold()warm()函数

相关问题 更多 >