A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 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)

  • This Question is Closed
  1. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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)

  2. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    err 3*x + 15 is actually how you'd write the arithmatic.

  3. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Thanks polpak.. hm. I tried it but it still doesn't work.

  4. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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]

  5. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    x = 100 divisors = [] for i in range (1, x): if x%i == 0: divisors = divisors + [i] hm... I don't get any response..

  6. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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).

  7. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    try print divisors

  8. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Thanks so much. You're right, I didn't tell it to do anything.

  9. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Let me know if you have more questions. Getting started in programming can be a bit challenging.

  10. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Thanks polpak. The courseware and openstudy is so useful.

  11. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  12. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

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

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

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.