학습목표: 팁을 계산해주는 프로그램 만들기
참고자료: 팁계산기 / 팁계산기정답 / 파이썬독스(부동소수점 산수) /
1. 팁 계산기를 만드시오
12% 는 백분율로 표현하면 0.12
150달러에 팁 12%를 더한 값은 = 150 * 1.12 으로 계산하면 됌
Instructions
#If the bill was $150.00, split between 5 people, with 12% tip.
#Each person should pay (150.00 / 5) * 1.12 = 33.6
#Format the result to 2 decimal places = 33.60
#Tip: There are 2 ways to round a number. You might have to do some Googling to solve this.💪
#Write your code below this line
Example Input
Welcome to the tip calculator!
What was the total bill? $124.56
How much tip would you like to give? 10, 12, or 15? 12
How many people to split the bill? 7
Example Output
Each person should pay: $19.93
2. 오답
줄바꿈해줄때 input("hello! world \nNice to meet you.") 큰 따옴표 1개로 묶어줘야함
float(total_bill / count_people) 이렇게 형변환 float 함수 안에서 연산할 수 없음. 나눗셈 연산을 형변환 못해주기때문
total_bill = input("Welcome to the tip calculator!"\n"What was the total bill? ")
percent_tip = input(f"How much tip would you like to give? {10, 12,} or {15}? ")
count_people = input("How many peole to split the bill? ")
n1 = float(total_bill / count_people)
print(n1)
plus_tip = float(percent_tip) / 100 + 1
float(total_bill / count_people) * float(percent_tip) / 100 + 1
3. 답
#round( ) 함수에 담긴 부동소수형태의 데이터를 소수점 2째까지만 살리고 반올림해줌
total_bill = input("Welcome to the tip calculator!"\n"What was the total bill? ")
percent_tip = input(f"How much tip would you like to give? {10, 12,} or {15}? ")
count_people = input("How many peole to split the bill? ")
n1 = float(total_bill) / int(count_people)
plus_tip = float(percent_tip) / 100 + 1
result = round(n1 + plus_tip , 2)
print(f"Each person should pay: $ {result}")
# 실행결과: $ 22.55
4. 모범 답안
final_amount = "{:0.2f}".format(bill_per_person) 으로 대체할 수 있다.
bill_per_person은 부동소수타입인데 .format(bill_per_person)이 형변환을 str 으로 해주고,
문자열 "{:0.2f}" 는 소수점 아래 2째 자리까지의 형태로 나타내게 한다.
print("Welcome to the tip calculator!")
bill = float(input("What was the total bill? $"))
tip = int(input("How much tip would you like to give? 10, 12, or 15? "))
people = int(input("How many people to split the bill?"))
tip_as_percent = tip / 100
total_tip_amount = bill * tip_as_percent
total_bill = bill + total_tip_amount
bill_per_person = total_bill / people
final_amount = round(bill_per_person, 2)
#final_amount에 빌 퍼 퍼슨을 반올림해서 소수점 2째자리까지 표현하는 코드들
#final_amount = "{:.2f}".format(bill_per_person)
#final_amount = "{:0.2f}".format(bill_per_person)
#final_amount = "{:.2f}".format(round(bill_per_person))
#final_amount = "{:.2f}".format(round(bill_per_person, 2))
#final_amount = "%.2f" % bill_per_person
# FAQ: How to round to 2 decimal places?
# Find the answer in the Q&A here: https://www.udemy.com/course/100-days-of-code/learn/lecture/17965132#questions/13315048
print(f"Each person should pay: ${final_amount}")
#유데미 #유데미코리아 #스타트위드유데미 #스터디윗미