A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 4 years ago

Hi, I'm on ps1b, can't figure out why my code won't take the sum of my changing variable ln. Thanks! prime=3.0 divisor=2.0 pcount=2.0 while pcount<1001.0: while (divisor<prime): if prime%divisor==0.0: prime=prime+2.0 divisor=2 elif prime%divisor!=0.0: divisor=divisor+1.0 if divisor==prime: import math ln = (math.log(prime,2.7182818284)) print sum (ln)/prime prime=prime + 2 divisor=2

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

    3rd line from the bottom, the sum (ln) part is messed up, the error says, print sum (ln)/prime TypeError: 'float' object is not iterable

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

    sum is designed to add the elements of a list. Here ln is a scalar (a single value), not a list. Addition in Python is done using the + operator.

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

    if the variable ln is changing every time, how would you set that up? would it add the previous natural logarithims of the variable 'prime' to the already existing one?

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

    Here are a couple suggestions. If you want to keep a running sum in a loop, begin by defining that variable outside of the loop. It makes easier reading. For example: sum = 0 for x in range(1,10): sum = sum + x print sum At the end, 'sum' is the sum of the digits 1 through 9. Only put things in the loop that need to happen on every pass through the loop. For example, move 'import math' to the top of your program because it only needs to happen once.

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

    i didnt understand/realize how to use the for loop. think I got it though, thanks!

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