簡體   English   中英

Function 從列表的正值計算平均溫度,除非在列表中找到溫度 >= 哨兵值。 兩個參數

[英]Function to calculate average temperature from positive values of a list unless a temp >= the sentinel values if found in the list. Two parameters

calculate_average_positive_temperature() function 采用兩個參數:

integer 溫度列表稱為 temperature_list。 一個 integer sentinel_value。

計算並返回 temperature_list 中所有正溫度的平均值,直到遇到大於或等於 sentinel_value 的溫度。

- 如果在 temperature_list 中沒有找到大於或等於 sentinel_value 的溫度,則應返回 temperature_list 中所有正溫度的平均值。 - 如果 temperature_list 中沒有正溫度,或者如果在遇到正溫度之前達到或超過 sentinel_value,則 function 應返回 0.0 作為平均溫度。 - 就本 function 而言,0 溫度應計為正溫度。 -四舍五入到小數點后兩位

所需的條件對我來說還不夠清楚,但從我收集到的信息來看,這是可行的。 Go 提前並根據需要添加更多。 我已經將每個條件的作用作為注釋放在代碼中。

i) 如果沒有正溫度或所有溫度都超過標記值,則返回 0.0

ii) 如果沒有值 >= sentinal 值,則返回正溫度的平均值

def calculate_average_positive_temperature(temp_list, sent_value):
    if max(temp_list) < 0 or min(temp_list) > sent_value: 
        return 0.0 # Returns 0 if no positive temp. or if no temp. is less than sentinel value

    elif max(temp_list) < sent_value:
        positive_temp_list = [temperature for temperature in temp_list if temperature >= 0]

        # returns average of positive temperatures if no temperature is greater than sentinel value
        return sum(positive_temp_list)/len(positive_temp_list)

x  = calculate_average_positive_temperature(temperature_list, sentinel_value)
print(x)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM