anonymous
  • anonymous
There was a code presented in lecture 3, but I don't know why it won't work in my Python interpreter.... Here's the code: x = 100 divisors = () for i in range (1, x): if x%i == 0: divisors = divisors + (i)
MIT 6.00 Intro Computer Science (OCW)
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
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
  • schrodinger
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
anonymous
  • anonymous
it has to do with a funky corner case in the python language. A tuple with no elements is (), a tuple with two elements is (a,b), but a tuple with only one element is (a,) The comma is required to disambiguate it from the case where you just have an expression you want to evaluate first. (x+3)*5 means, take the sum of x and 3, then multiply by 5. Without the parens you'd have to write it as x+15 which is not as expressive. But this operation does not involve tuples, however if a tuple with a single element was constructed with (a) there would be no way to tell when you wanted a tuple and when you didn't. The alternative is to not use the tuple literal syntax, and call tuple explicitly. x = 100 divsors = tuple() for i in range(1,x): if x%i == 0: divisors = divisors + tuple(i)
anonymous
  • anonymous
err 3*x + 15 is actually how you'd write the arithmatic.
anonymous
  • anonymous
Thanks polpak.. hm. I tried it but it still doesn't work.

Looking for something else?

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

More answers

anonymous
  • anonymous
Lol, and my code doesn't work either. should be tuple([i]), which is not very expressive. Honestly I don't know why tuples are being used in this example at all. Use lists. They work the way you'd expect. replace the () and (i) in your code with [] and [i]
anonymous
  • anonymous
x = 100 divisors = [] for i in range (1, x): if x%i == 0: divisors = divisors + [i] hm... I don't get any response..
anonymous
  • anonymous
nor should you. You've constructed a list of divisors. You can now either print that list, or do something else with it. The code you've written here doesn't display anything, it just does what you asked it to do (construct a list).
anonymous
  • anonymous
try print divisors
anonymous
  • anonymous
Thanks so much. You're right, I didn't tell it to do anything.
anonymous
  • anonymous
Let me know if you have more questions. Getting started in programming can be a bit challenging.
anonymous
  • anonymous
Thanks polpak. The courseware and openstudy is so useful.
anonymous
  • anonymous
Actually, if you wanted to use the tuples instead, then the correct syntax should be: x = 100 divisors = tuple() for i in range(1,x): if x%i == 0: divisors = divisors + (i,) print divisors You needed the code written like this divisors = divisors + (i,) in order for it to work. I tested it and it works fine, resulting in identical values. Though I am a bigger fan of lists as well.
anonymous
  • anonymous
I did to. Aslander that is what I used. Once you run it you have to type divisors in order for them to come up.

Looking for something else?

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