I'm an absolute beginner in Python, doing the following training task:
"Two friends are eating dinner at a restaurant, the bill comes in the amount of 47.28 dollars. The friends split the bill evenly between them, after adding 15% tip for the service. Calculate the tip, the total amount to pay, and each friend's share, then output a message saying "Each person needs to pay: " followed by the resulting number".
I wrote the code given below but the system is saying there is a calculation error there. What exactly can be the error?
bill = 47.28
tip = 15 // bill * 100
total = bill tip
share = total // 2
print("Each person needs to pay:" str(share))
CodePudding user response:
// is a floored division, ie it will round down the result of the division to the nearest integer. If the numerator is smaller than the denominator, then you’ll get zero as a result of the floored division. Try tip = bill * 0.15
instead to calculate the tip.
CodePudding user response:
Premise: please let me suggest to use a different approach to programming challenges. If you're not able to do the dirty work and sweating to solve such a basic problem your career won't be long. Please take your time to search, try, test, google before coming here asking just "PLEASE HELP". You'll gonna learn a lot more and you'll be surprised by your own problem solving attitude. Believe me, please.
Then you can solve it a little more elegantly with a function:
def split_bill_calculator(invoice:float, taxes:int, people:int)-> float:
"""
Invoice: FLOAT value
Taxes: INTEGER percent value
People: INTEGER
Return: total amount splitted for each person
"""
add_tip = invoice * (1 taxes / 100)
payable_by_each = round(add_tip / people, 2)
print(f"Each of the {people} guests should pay {payable_by_each}")
return payable_by_each
CodePudding user response:
invoice = 47.28
taxes_percent = 15
tip = (invoice * taxes_percent) / 100
total_with_tip = invoice tip
each_person = total_with_tip / 2
print(round(each_person,2))
CodePudding user response:
bill = 47.28
tip = (bill *15) //100
total = bill tip
share = total // 2
print("Each person needs to pay:" str(share))