0

So here is the problem: The even_or_odd_all function takes as input a list of integers and computes and returns a list containing True/False representing whether each corresponding number in the input list is even.(use While loop)

This is my code:

    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

however I also keep getting this error

  { 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}
K P
  • 19
  • 3

2 Answers2

1

you needed to just create a list and you can use a for loop to iterate through the odd numbers and even numbers list and append the condition to the list and return it.

here's the code:

 def even_or_odd_all(even_odd):
        bool_list = []
    
        for i in even_odd:
            bool_list.append(i % 2 == 0)
        return bool_list

Call the function: even_or_odd_all([2,4,6,12,12,312,31,2312])

Output: [True, True, True, True, True, True, False, True]

Dharman
  • 30,962
  • 25
  • 85
  • 135
1

Via list comprehension your problem is literally a one-liner:

def even_or_odd_all(even_odd):
    return [n % 2 == 0 for n in even_odd]

A while loop is not really a good choice as you want to iterate over a list and this always invites for a for-construction.

matheburg
  • 2,097
  • 1
  • 19
  • 46