我已经编写了一个程序,从用户那里获取摄氏度的输入,并将其转换为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))
在您的while之前有一行无用的代码,可以删除:
以下是您的代码版本,可满足您的要求:
因此,您的代码中存在多个问题:
input
statmenet,是什么导致了一个您没有做任何处理的额外输入warm
中的条件是错误的,没有temps[far] is (>= 50 or < 85):
因此,代码的工作版本是:
首先,总是把函数去宽容放在主函数之上,这是一个很好的实践
其次,
cold
/warm
将获得一个浮点值,并检查单个元素,而不是整个列表。我认为你的情况更好,但这要看情况而定第三,通过使用
map
和返回值是布尔值(解释为0/1)这一事实,我们可以sum
并获得所需的结果最后,请阅读^{} 。当您希望打印多个带有变量等的内容时,它可能很方便
由于您可能不熟悉
map
,这里有一个link来阅读您声明了函数,现在必须在其中写入逻辑,这将帮助您计算温暖和寒冷天气的值。请看下面的代码
编辑:
除了使用冷函数和热函数,您还可以使用lamba函数,如下所示:
而不是
现在您可以删除
cold()
和warm()
函数相关问题 更多 >
编程相关推荐