Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 15 Online
OpenStudy (anonymous):

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

OpenStudy (anonymous):

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

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

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?

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

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

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!