Open study

is now brainly

With Brainly you can:

  • Get homework help from millions of students and moderators
  • Learn how to solve problems with step-by-step explanations
  • Share your knowledge and earn points by helping other students
  • Learn anywhere, anytime with the Brainly app!

A community for students.

If I want to write a program that checks if a % list of numbers == 0 how would I write the list of numbers? Would I use a tuple, string, or ?

MIT 6.00 Intro Computer Science (OCW)
I got my questions answered at in under 10 minutes. Go to now for free help!
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.

Join Brainly to access

this expert answer


To see the expert answer you'll need to create a free account at Brainly

Typically, you wouldn't use a string to hold a bunch of numbers. First, you need to decide if the list of numbers is mutable or not (mutable = changeable). If it IS mutable (you need to be able to add, remove or change values) use a list. If it isn't mutable ... use a tuple. pseudocode for this (please note that I didn't actually test this as valid code, just trying to give an overview of the concept) numberTuple = (1, 2, 3, 4, 5, 6) for (individualNumber) in numberTuple: if individulaNumber == 0: print "this number equals zero"
If I understand you well, you want to check if some number a is divisible by numbers in the list? It will be: for numer in number_list: if a % number == 0: print a, 'is divisible by', number Or, to check if a is divisible by every number in the list: def is_divisible(a, number_list): # function returns True if number a is divisible by every number in the list # else returns false for number in number_list: if a % number != 0: return False return True
Ah yes, pedja is correct ... I totally missed that a was a variable and just read it as the English word "a" and figured the % was a typo, silly me. You'd want to store the numbers in a list or tuple, then do exactly what pedja said and run a for loop over the list, checking each of the values in the list in turn

Not the answer you are looking for?

Search for more explanations.

Ask your own question

Other answers:

hmmmm... well I think I don't understand the syntax. Here is my program: I know that there is a better way to do this with way more fancy math and the program that I am trying to write will be slow, ect. However at this point I find it is better for me to learn the syntax using my own thoughts and ideas, rather than math that is complex. Once I can actually write something that doesn't take me hours to do then I will come back and complete the psets with different and more complex ideas. "steps off soap box* :P When I run this program it is not dividing the guess by every number in the number_list and print a result "composite number: " , guess. Rather it is taking the guess and dividing it by an integer in what seams to be a range (1, ?,1).
Consider how you're defining a composite number ... At line 8: if guess % number == 0: print 'composite number: ', guess Which translates to -- If the remainder of guess divided by number equals zero, this is a composite number. Which simply isn't true ... case in point (3). The remainder of three divided by three is zero so it passes the if clause on line 8 and is declared a composite number erroneously (it's prime not composite). Consider also the number 6 ... the remainder of six divided by six is also zero, yet this is a composite number. Consider testing instead to determine if it's a prime number.
Jesse - this is my take on how I'd handle the problem. Sorry if it got a bit mathy. I tried to comment the livin heck out of it to explain why I made the decisions I did.
I like the way that you did it. I understand it perfectly. I am still trying to make my program work unfortunately. This is where I am at this time: I did a prt scr of what I am getting to show you the problem, I just don't know how to fix it. It is the same as before. When I run this program it is not dividing the guess by every number in the number_list and print a result "composite number: " , guess. Rather it is taking the guess and dividing it by an integer in what seams to be a range (1, ?,1).
1 Attachment
Line nine ... if guess == number If that line is true then you know for a fact that it's a prime because it's in your explicit list of prime numbers and you're done checking that number You don't need the if/else after that. You just need to pick out the primes based on whether they're in your list, print them when you find them, consider everything else composite I think this might be what you're going for --
Oh my, I look like a major dum dum. lol I think I honestly stared at it so long my thoughts were turning to mush. And now that I am able to see the last post it shows how silly the original thought was. I still wonder why it was running through every integer rather than the list, but I'm just going to let that go for now. Again, you rock! Much love!
*grins* not dumb at all. We all have moments of staring at something so long our eyes cross and we just stop seeing it. Were I to make a guess, I'd venture that it was running through every integer because you used "pass" on line 12 instead of "break" which would break you out once you found a prime. So it would find the actual prime in the first if, then for non-primes drop into the else (which potentially reports false primes 'cause the prime check down in the else can't possibly find a prime 'cause they were all found in the initial if)

Not the answer you are looking for?

Search for more explanations.

Ask your own question