티스토리 뷰
반응형
유데미의 명강의 중 하나인
안젤라 유(Angela Yu) 선생님의 '파이썬 부트캠프: 100개 프로젝트로 파이썬 개발 완전정복' 강의를 듣고 있어 재밌는 부분은 종종 기록으로 남겨볼 생각이다. 2일차 강의에서 배운 것을 총 망라하는 마지막 과제는 '팁 계산기 만들기(tip-calculator)'
데이터 형식과 문자열을 조작하는 방법에 대해 배웠고, 이를 활용해 팁을 계산하는 식을 녹여낸 코드를 작성했다.
내가 작성한 것과 강의에서 제공된 정석 코드를 비교해보니 확실히 다르다.
시간이 좀 걸리더라도, 변수명만 보고도 이해할 수 있게끔 잘 정리된 코드를 연습해야 할 것 같다.
(파이썬 강의 출처: https://www.udemy.com/course/best-100-days-python/learn/lecture/29148976#overview)
1. 내가 작성한 팁계산기 코드
#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 👇
print("Welcome to the tip calculator")
bill = input("What was the total bill?")
bill_as_int = float(bill)
tip = input("What percentage tip would you like to give? 10, 12, or 15?")
tip_as_int = int(tip)
tip_as_float = float(tip_as_int/100)
tip_plus_1 = (tip_as_float + 1)
total_bill = bill_as_int * tip_plus_1
people = input("How many people to split the bill?")
people_as_int = int(people)
each_pay = round(total_bill/people_as_int,2)
print(f"Each person should pay: ${each_pay}")
2. 강의 정답 코드 (깔끔, 정석 그 자체 ㅋㅋㅋ)
#If the bill was $150.00, split between 5 people, with 12% tip.
#Each person should pay (150.00 / 5) * 1.12 = 33.6
#Round the result to 2 decimal places.
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)
# 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}")
2일차 강의에서 배운 것
- 파이썬은 직관적으로 수학적 표현식을 사용할 수 있는 프로그래밍 언어다. 지수 표현도 가능하고, 심지어 엑셀처럼 round() 함수를 이용해 소수점 몇째자리까지 반올림할지도 표현할 수 있다.
- 여러가지 수식을 이용하여 계산 후, 특정 결과를 리턴해야 할때는 한번에 결과를 내려하지 말고 계산하는 과정마다 print() 함수를 써서 내가 맞는 방향으로 가고 있는지 찍어보는 것이 중요하다. input으로 받아오는 값들의 형식이 각기 다를 수 있는데 (int, float, boolean 등), 이 때는 print(type()) 안에 값을 넣어 찍어보는 것도 좋다.
- f-string: 문자열 형식 안에 int 등 여러 데이터 형식을 표기하고 싶을때 사용하는 방법으로 print(f"You have to pay {x} dollars")와 같이 문자열("") 형식 앞에 f를 붙이는 형태이며, 해당 문자열 안에 중괄호 {}를 활용해 다른 데이터형식의 값을 넣을 수 있다.
'study' 카테고리의 다른 글
[파이썬] 4강 랜덤 모듈과 리스트 함수로 가위바위보 게임 만들기_코드 비교 (2) | 2024.03.17 |
---|---|
[파이썬] 3강 논리 연산자_코드 비교 (2) | 2024.03.16 |
Q-learning on a windy frozen lake! (0) | 2023.10.14 |
Q-learning, Exploitation & Exploration (0) | 2023.10.12 |
CNN — ConvNet Max pooling 과 Full Network (0) | 2023.10.09 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 아야진해변
- 파이썬강의소개
- higher lower game
- 큐러닝
- 안젤라유강의
- 파이썬안젤라유
- 파이썬 안젤라유 강의
- 파이썬 게임 만들기
- 안젤라유파이썬
- 파이썬 초급강의
- 파이썬안젤라유강의
- 파이썬디버거
- 벡터
- 파이썬디버깅
- 프랑스어문법
- 불어문법
- 숫자업다운게임
- 파이썬반복문
- 파이썬thonny
- 파이썬for문
- higherlower게임
- 유데미파이썬강의
- 반과거
- 파이썬전역범위
- 파이썬초급강의
- 고성
- 선형대수
- 복합과거
- 아야진
- qlearning
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
글 보관함