【发布时间】:2021-05-01 04:45:58
【问题描述】:
所以问题来了:even_or_odd_all 函数将整数列表作为输入,计算并返回一个包含 True/False 的列表,表示输入列表中的每个对应数字是否为偶数。(使用 While 循环)
这是我的代码:
def even_or_odd_all(even_odd):
#input is a list of integers
#output is a list of boolean values (which depends on if the number is even or odd)
#So this function is supposed to take in a list and return a list with booleans depending on if therye even or odd
# i guess i needa use a while loop
while True:
i in (range(len(even_odd)))
even_odd = []
if (i % 2) == 0:
even_odd.append(i % 2 == 0)
return [i]
i = False
但我也不断收到此错误
{ Traceback (most recent call last):
File "<input>", line 1, in <module>
File "<input>", line 7, in even_or_odd_all
UnboundLocalError: local variable 'i' referenced before assignment}
【问题讨论】:
-
您可以在此处尝试列表推导式,它可以为您提供一个新的布尔值列表,具体取决于您的输入列表。
标签: python-3.x math pycharm