Air pollution index calculator

Viewed 28

I got this question as an assignment: An Air Pollutant Index (API) recording system should allow user to enter the name of month to start using the system. If the user entered September, April, June, or November as the month, 30 API readings should be keyed-in by the user into the program. If user entered February, the system will ask the user to choose between leap year or non-leap year. If user choose non leap year, 28 API readings should be keyed-in into the system or else, 29 API readings will be captured. Other than that, 31 readings should be keyed-in by the user. For each API reading entered, the program should be able to analyze the condition and display the API reading with its status as per in Figure 1.

Here's what I have tried and I can't figure it out anymore, please help; '''

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'
1 Answers
for api in api_input:
        if api >= 0 and api <= 50:
            return "Good"

api is an element of api_input. In your code api_input contains lists for e.g. api_input_30 is a list, therefore each instance 'api' is actually a list and you can't compare a list to an integer as the TypeError says.

Related