Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing


  • 2 years ago

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?

  • This Question is Closed
  1. Denu8
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    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.

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

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy