0

I just started learning about classes and am trying to build a calculator that tells people how much they need to tip the waiter as a small project but instead of entering myself the information i want that a user will do it himself so it will suit hes needs. now i think i built it right, the computer accepts the inputs but when i try to call the return_answer() function i get an error that says: AttributeError: tip_calculator object has no attribute 'return_answer' can someone please explain to me what i am doing wrong and how to fix it? thanks in advance :)

class tip_calculator:
    def __init__(self,bill,amount_of_diners,precent):
        self.bill = bill
        self.amount_of_diners = amount_of_diners
        self.precent = precent

        def return_answer(self):
            print("The amount you need to pay is: ", precent * bill / amount_of_diners ** bill * 1000)

calc = tip_calculator(int(input("What was the bill?: ")), 
                      int(input("With how many people did you eat?: ")),
                      int(input("what '%' do you want to give the waiter?: ")))

calc.return_answer()

1 Answer 1

2

The problem is with the indentation:

class tip_calculator:
    def __init__(self, bill, amount_of_diners, precent):
        self.bill = bill
        self.amount_of_diners = amount_of_diners
        self.precent = precent

    def return_answer(self):
        print(
            "The amount you need to pay is: ",
            self.precent * self.bill / self.amount_of_diners ** self.bill * 1000,
        )


calc = tip_calculator(
    int(input("What was the bill?: ")),
    int(input("With how many people did you eat?: ")),
    int(input("what '%' do you want to give the waiter?: ")),
)

calc.return_answer()

Your return_answer method was indented wrongly inside the initialiser (__init__). Also, in your return_answer, when you want to access class members like the fields bill, amount_of_diners and percent you need to do it with self because they are members of your class and not local variables to your method.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.