<p>您声明了函数,现在必须在其中写入逻辑,这将帮助您计算温暖和寒冷天气的值。请看下面的代码</p>
<pre><code>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)))
</code></pre>
<p><strong>编辑:</strong></p>
<p>除了使用冷函数和热函数,您还可以使用lamba函数,如下所示:</p>
<pre><code>print("Cold days: " + str(sum(value < 50 for value in temps)))
print("Warm days: " + str(sum(85 > value >= 50 for value in temps)))
</code></pre>
<p>而不是</p>
<pre><code>print("Cold days: " + str(cold(temps)))
print("Warm days: " + str(warm(temps)))
</code></pre>
<p>现在您可以删除<code>cold()</code>和<code>warm()</code>函数</p>