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
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
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.
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?
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.
i didnt understand/realize how to use the for loop. think I got it though, thanks!
Join our real-time social learning platform and learn together with your friends!