Quantcast

A community for students. Sign up today!

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

kah712

  • one year 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
    • one year 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.

    Search OpenStudy
    • Attachments:

Ask your own question

Ask a Question
Find more explanations on OpenStudy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.