yhlee: Alto clef and whole note (middle C). (Default)
yhlee ([personal profile] yhlee) wrote in [community profile] intro_to_cs 2009-11-17 02:15 am (UTC)

Let's see--
Two things:

1. First thing to try is replace
log
with
math.log
(I couldn't get the former to work for me and don't know whether it's a version-based syntax change or an error or what).

2. Also, from your code:
sum = sum(log(primes)) # this is where I get an error message

First, you've defined
primes
earlier thus:

primes = (2, 3, 5, 7, 11, 13) # this is just as a test case

I'd have to look up the syntax, but that means
primes
is a tuple. I don't believe that
log(primes)
will work because
log
(or
math.log
) needs to operate on a float, it can't operate on the whole tuple--it sees this tuple of 2 3 5 7 11 13 and doesn't know which one to operate on. (It would be fairly easy to define a function to take a tuple of numbers > 0 and return another tuple containing the logs of those numbers, though.)

Also, I'm a little confused by your use of
sum
--did you previously initialize it to something not excerpted in your example code? Because
sum = sum(log(primes)) # this is where I get an error message

makes it look like
sum
is a function acting on something, and then the function is being assigned to itself. Which I guess you could do, but I don't think we've hit recursion yet (and that might not be the syntax to do it in Python anyway).

Ping me if you have any other questions (assuming someone more knowledgeable doesn't get to it first, of course), but hopefully that'll get you started.

Post a comment in response:

This community only allows commenting by members. You may comment here if you're a member of intro_to_cs.
(will be screened if not on Access List)
(will be screened if not on Access List)
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting