0

I keep getting TypeError: 'float' object is not subscriptable wondering why

from math import log

class Logarithm(object):

    def __init__(self, base = 0, number= 0):
        self.base = float(base)
        self.number = float(number)

        the_logarithm = log(self.base[self.number])

    def __str__(self):
        return 'Your log = {}'.format(the_logarithm)
1
  • also, it should be self.the_logarithm (both in __init__ and in __str__). Commented May 6, 2013 at 23:09

2 Answers 2

2

Cameron Sparr's answer is correct.

You should probably re-check the help(math.log). It is

log(x[, base]) -> the logarithm of x to the given base.

meaning that the base argument is optional (defaults to e) and not log(x[base])

Sign up to request clarification or add additional context in comments.

Comments

2

Because of this:

log(self.base[self.number])

What are you trying to accomplish here? self.base is a float so this statement is being evaluated as "the numberth element of base", which Python can't do.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.