Is there any way to reflect the value of n in cost function globally . so that the value of n in getLeastCost function with be changed instead of n = 0

class TheShuttles:
        def getLeastCost(self,baseCost,seatCost,cnt):
            n = 0
            def cost(x):
               n = sum( m / x + (m % x != 0 ) for m in cnt)
               return n * (baseCost + x * seatCost) 
               print n { prints 0 }
            return min( cost(x) for x in range(1,101) )

We don't know what n is. The easiest way is to make it an attribute of a TheShuttles instance.

class TheShuttles:
    def __init__(self):
        self.n = 0

    def getLeastCost(self, baseCost, seatCost, cnt):
        def cost(x):
            self.n = sum(m / x + (m % x != 0 ) for m in cnt)
            return self.n * (baseCost + x * seatCost)
        return min(cost(x) for x in range(1, 101))

Also, the cost function could be a method of class TheShuttle instead of a function inside a function, it would be easier to understand. Type import this in a python console for good advice.

Configure your code editor to indent python code with 4 spaces, and read PEP8 for help on coding style.

EDIT: In python 2, make sure to declare a superclass, use object if necessary:

class TheShuttles(object):
    ...

In python 3, this is not needed. If you are new to python, start with python 3.

Thank you for many tips and advice

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.