【发布时间】: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