Python 如何将Celcius转换为Farenheit,然后根据用户输入的温度计算寒冷和温暖的天数
我已经编写了一个程序,从用户那里获取摄氏度的输入,并将其转换为Farenheit。我把大部分节目都录下来了 我只是不知道如何让它输出寒冷天气和温暖天气的发生次数,这取决于用户输入Python 如何将Celcius转换为Farenheit,然后根据用户输入的温度计算寒冷和温暖的天数,python,python-3.x,Python,Python 3.x,我已经编写了一个程序,从用户那里获取摄氏度的输入,并将其转换为Farenheit。我把大部分节目都录下来了 我只是不知道如何让它输出寒冷天气和温暖天气的发生次数,这取决于用户输入 #此程序将10个条目从摄氏度转换为法伦海特。 打印('让我们将过去10天的温度从摄氏度转换为法伦海特') #创建开放列表和循环,提示用户输入法伦海特的所有温度。 temps=list() 摄氏度=整数(输入(“以摄氏度输入过去10天每天的温度:”) 而len(临时工)!=10: 临时追加(摄氏度) 摄氏度=整数(输入(
#此程序将10个条目从摄氏度转换为法伦海特。
打印('让我们将过去10天的温度从摄氏度转换为法伦海特')
#创建开放列表和循环,提示用户输入法伦海特的所有温度。
temps=list()
摄氏度=整数(输入(“以摄氏度输入过去10天每天的温度:”)
而len(临时工)!=10:
临时追加(摄氏度)
摄氏度=整数(输入(“以摄氏度输入过去10天每天的温度:”)
打印(“好的,过去10天的温度,单位为:,摄氏度)
#使用for循环,将用户的每个条目转换为摄氏度并打印结果。
对于远距离(len(temps)):
temps[far]=(temps[far]*1.8)+32
打印(“过去一周每天的温度,换算成法伦海特,为:,temps)
def cold():
如果温度[far]<50:
打印(len(temps[far]))
如果寒冷:
印刷(透镜(临时)
def warm():
如果温度[far]为(>=50或<85):
打印(len(temps[far]))
如果温度较高:
印刷(透镜(临时)
您声明了函数,现在必须在其中编写逻辑,这将帮助您计算温暖和寒冷天气的值。请看下面的代码
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)))
现在您可以删除
cold()
和warm()
函数。在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))
def冷态(温度):
计数=0
对于温度(以温度为单位):
如果温度=50且温度,则代码中存在多个问题:
您使用的是范围变量,而不是将变量传递给函数
在此之前,您有一个额外的input
statmenet,是什么导致了您没有使用的额外输入
您在warm
中的条件错误,没有temps[far]is(>=50或<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))
def冷态(T):
返回T<50
def暖机(T):
返回T>=50和T如果[function name]
,则可以说,如果定义了函数,则将其视为布尔值,如果未定义,则抛出namererror
。
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))
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))