1

I keep facing a problem when I try to calculate the total cost of a meal. I can't seem to find a solution to this.

Sample input:

  • meal_cost: 12.00
  • tip_percent: 20
  • tax_percent: 8

Currently, the program prints this: "<function solve at 0x00000283C7ED4E50>" (the result supposed to be 15)

def solve(meal_cost, tip_percent, tax_percent):
    tip = meal_cost * (tip_percent / 100)
    tax = meal_cost * (tax_percent / 100)
    totalCost = round(meal_cost + tip + tax)
    return totalCost

if __name__ == '__main__':
    meal_cost = float(input())

    tip_percent = int(input())

    tax_percent = int(input())

    solve(meal_cost, tip_percent, tax_percent)
    print(solve)

2 Answers 2

1

It's the line where you print the result actually. When you print(solve) instead of print(solve(meal_cost, tip_percent, tax_percent)), you are printing the function instead of the result of the function call.

See the correct version below:

def solve(meal_cost, tip_percent, tax_percent):
    tip = meal_cost * (tip_percent / 100)
    tax = meal_cost * (tax_percent / 100)
    totalCost = round(meal_cost + tip + tax)
    return totalCost

if __name__ == '__main__':
    meal_cost = float(input())

    tip_percent = int(input())

    tax_percent = int(input())

    result = solve(meal_cost, tip_percent, tax_percent)
    print(result)
Sign up to request clarification or add additional context in comments.

Comments

1

The answer provided by BedirYilmaz is correct and should be preferred. However, those more familiar with programming in Python could for some reason want to use functions the way you thought they worked. The function could be called, and then the name of the function could be used to get the return value as a string. If this is being done in a single-threaded program, the following example class ReturnWrapper should be sufficient for use as a function decorator:

class ReturnWrapper:
    def __init__(self, function):
        self.__function = function
        self.__value = function

    def __repr__(self):
        return repr(self.__value)

    def __str__(self):
        return str(self.__value)

    def __call__(self, *args, **kwargs):
        self.__value = self.__function(*args, **kwargs)
        return self.__value


def main():
    meal_cost = float(input())
    tip_percent = int(input())
    tax_percent = int(input())
    solve(meal_cost, tip_percent, tax_percent)
    print(solve)


@ReturnWrapper
def solve(meal_cost, tip_percent, tax_percent):
    tip = meal_cost * (tip_percent / 100)
    tax = meal_cost * (tax_percent / 100)
    total_cost = round(meal_cost + tip + tax)
    return total_cost


if __name__ == '__main__':
    main()

If the actual return value needed to be retrieved after running the function, a value property would be very helpful on the class. Since it is rather simple to implement, it is left as an exercise for the reader to complete.

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.