Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 19 Online
OpenStudy (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?

OpenStudy (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.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!