• anonymous
List and Boolean output: The exercise gives you a list and the answer, I must find the missing code: >>>a_list = [3,5,6,12] >>>#CODE HERE [False, False, True, True] I made a for loop: for x in a_list: if x % 2 == 0: print True else: print False but my output prints as seperate elements: False False True True How do I write the code so that it prints the booleans as a list?
MIT 6.00 Intro Computer Science (OCW)
  • Stacey Warren - Expert
Hey! We 've verified this expert answer for you, click below to unlock the details :)
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
  • schrodinger
I got my questions answered at in under 10 minutes. Go to now for free help!
  • anonymous
Couldn't you just append it to a list, and then print the list? a_list = [3,5,6,12] boolean_list=[] for x in a_list: if x % 2 == 0: boolean_list.append(True) else: boolean_list.append(False) print boolean_list Another way to do it would be to smash it into a string and use list-type syntax, but it's a lot harder and generally not worth the extra code. It would work something like this though: a_list = [3,5,6,12] s = '[' for x in a_list: if x % 2 == 0: s += str(True) + ',' else: s += str(False) + ',' print s[0:len(s)-1] + ']' The first one works so much better though.

Looking for something else?

Not the answer you are looking for? Search for more explanations.