user2356379
user2356379

Reputation: 1

TypeError: 'float' object is not subscriptable Python 3

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)

Upvotes: 0

Views: 4549

Answers (2)

Cameron Sparr
Cameron Sparr

Reputation: 3981

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.

Upvotes: 2

nvlass
nvlass

Reputation: 685

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])

Upvotes: 2

Related Questions