【问题标题】:Air pollution index calculator空气污染指数计算器
【发布时间】:2022-10-05 14:10:22
【问题描述】:

我得到了这个问题作为作业: 空气污染物指数 (API) 记录系统应允许用户输入月份名称以开始使用该系统。如果用户输入 9 月、4 月、6 月或 11 月作为月份,则用户应将 30 个 API 读数键入到程序中。如果用户输入二月,系统会要求用户选择闰年或非闰年。如果用户选择非闰年,则应将 28 个 API 读数键入系统,否则将捕获 29 个 API 读数。除此之外,用户应键入 31 个读数。对于输入的每个 API 读数,程序应该能够分析条件并显示 API 读数及其状态,如图 1 所示。

这是我尝试过的,我无法弄清楚,请帮助; '''

def index_calculator():
    month = input("What month would you like to check?: ")
    api_input_30 = []
    api_input_31 = []
    api_input_29 = []
    api_input_28 = []
    api_input = [[api_input_30], [api_input_31], [api_input_29], [api_input_28]]
    if month == "september, sept, april, june, november, nov":
        api_input_30 = [int(input("api") for _ in range(30))]
        
    if month == "feb, february":
        leap_nleap = input("Type L for leap year and N for not leap year: ")
        if leap_nleap == "L":
            api_input_29 = [int(input("api")for _ in range(29))]
        elif leap_nleap == "N":
            api_input_28 = [int(input("api")for _ in range(28))]

    if month == "jan, january, mar, march, may, jul, july, august, aug, october, oct, dec, december":
        api_input_31 = [int(input("api") for _ in range(31))]
    
    for api in api_input:
        if api >= 0 and api <= 50:
            return "Good"
        if api > 50 and api <= 100:
            return "Moderate"
        if api > 100 and api <= 200:
            return "Unhealthy"
        if api > 200 and api <= 300:
            return "Very Unhealthy"
        else:
            return "Hazardous"
index_calculator()
'''
What month would you like to check?: nov
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [37], in <cell line: 32>()
     30         else:
     31             return "Hazardous"
---> 32 index_calculator()

Input In [37], in index_calculator()
     19     api_input_31 = [int(input("api") for _ in range(31))]
     21 for api in api_input:
---> 22     if api >= 0 and api <= 50:
     23         return "Good"
     24     if api > 50 and api <= 100:

TypeError: '>=' not supported between instances of 'list' and 'int'

【问题讨论】:

  • 如果您能向我们展示此列表的示例会更好:api_input_30 = []、api_input_31 = []、api_input_29 = []、api_input_28 = []。你想使用这个代码 api_input = [[api_input_30], [api_input_31], [api_input_29], [api_input_28]] 除了这个 api_input = [api_input_30, api_input_31, api_input_29, api_input_28]?
  • 列表应该具有用户输入的不同整数
  • 您想如何处理数字列表?因为在您的循环中,它只会使用列表中的第一个数字并返回结果。如果您想为每个数字返回结果,您应该创建一个结果列表,然后返回此列表。或者您可能想使用另一种算法?
  • 我稍微调整了一下,想出了一些可以工作的东西

标签: python


【解决方案1】:

对于 api_input 中的 api: 如果 api >= 0 且 api <= 50: 返回“好”

api 是 api_input 的一个元素。在您的代码中 api_input 包含例如列表api_input_30 是一个列表,因此每个实例 'api' 实际上是一个列表,您不能像 TypeError 所说的那样将列表与整数进行比较。

【讨论】:

  • 有没有办法访问嵌套列表的元素?
  • 您可以使用嵌套的 for 循环,但最佳做法是不要将月份元素放在嵌套列表中
【解决方案2】:
def index_calculator():
    month = input("What month would you like to check?: ")
    days = int(input("How many days are in this month?: "))
    leap_nleap = (input("Type L for leap year and N for non-leap year: "))
    api_input = [int(input("api: ")) for _ in range(days)]
    
    if leap_nleap == "L":
        days = 29
    elif leap_nleap == "N":
        days = 28
    
    for api in api_input:
        if api >= 0 and api <= 50:
            print("Good")
        elif api > 50 and api <= 100:
            print ("Moderate")
        elif api > 100 and api <= 200:
            print ("Unhealthy")
        elif api > 200 and api <= 300:
            print ("Very Unhealthy")
        else:
            print ("Hazardous")
index_calculator()

我想出了这段代码,它可以解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-10
    • 2014-07-06
    • 1970-01-01
    • 2011-06-04
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多