A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 5 years ago
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)
anonymous
 5 years ago
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)

This Question is Closed

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0it 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
 5 years ago
Best ResponseYou've already chosen the best response.0err 3*x + 15 is actually how you'd write the arithmatic.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Thanks polpak.. hm. I tried it but it still doesn't work.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Lol, 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
 5 years ago
Best ResponseYou've already chosen the best response.0x = 100 divisors = [] for i in range (1, x): if x%i == 0: divisors = divisors + [i] hm... I don't get any response..

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0nor 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
 5 years ago
Best ResponseYou've already chosen the best response.0Thanks so much. You're right, I didn't tell it to do anything.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Let me know if you have more questions. Getting started in programming can be a bit challenging.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Thanks polpak. The courseware and openstudy is so useful.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Actually, 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
 5 years ago
Best ResponseYou've already chosen the best response.0I did to. Aslander that is what I used. Once you run it you have to type divisors in order for them to come up.
Ask your own question
Sign UpFind 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
 Engagement 19 Mad Hatter
 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.