A community for students.
Here's the question you clicked on:
 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
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

anonymous
 4 years ago
Best ResponseYou've already chosen the best response.03rd line from the bottom, the sum (ln) part is messed up, the error says, print sum (ln)/prime TypeError: 'float' object is not iterable

anonymous
 4 years ago
Best ResponseYou've already chosen the best response.0sum 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.

anonymous
 4 years ago
Best ResponseYou've already chosen the best response.0if 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?

anonymous
 4 years ago
Best ResponseYou've already chosen the best response.0Here 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.

anonymous
 4 years ago
Best ResponseYou've already chosen the best response.0i didnt understand/realize how to use the for loop. think I got it though, thanks!
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.