gerdtf
gerdtf

Reputation: 705

Python max function does not work within my own function / TypeError: 'bool' object is not callable

Hey I am trying to print out the max (or min) of a list

The code in Python is the following:

def printDays(result):
    days=result[1]
    max=result[2]
    budget=result[3]
    if max==True: 
        cost=max(result[0])
    else:
        cost=min(result[0])
        
    template="You can spend $ {} in {} days with a $ {} limit"
    print(template.format(cost,days,budget))

"result" is a Tuple and "cost[0]" is a list with Integers (however they are all class types like instead of just tuple - really just starting here and no idea if it makes a difference). I have checked so much with printing out the type.

I constantly get a TypeError: 'bool' object is not callable

pointing to the line: cost=max(result[0])

Any idea why?

Upvotes: 0

Views: 145

Answers (2)

Nothing special
Nothing special

Reputation: 415

You have reinitialised the existing function max to result[2]. Anyways I've made your code a bit more pythonic.

def printDays(result):
  days, maximum, budget = result[1:4]
  if maximum: 
    cost=max(result[0])
  else:
    cost=min(result[0])
  #Better use a ternary. cost = max(result[0]) if maximum else min(result[0]) 
  template="You can spend $ {} in {} days with a $ {} limit"
  print(template.format(cost,days,budget))
  

Upvotes: 0

I'mahdi
I'mahdi

Reputation: 24069

your problem because you assign result[2] to max and call max, you can change your code like below:

def printDays(result):
    days=result[1]
    max_min_checker = result[2]
    budget=result[3]
    if max_min_checker==True: 
        cost=max(result[0])
    else:
        cost=min(result[0])

    template="You can spend $ {} in {} days with a $ {} limit"
    print(template.format(cost,days,budget))

Upvotes: 1

Related Questions