본문 바로가기
개발일지/Python

2022.01.13 유데미 파이썬 부트캠프 Day-2

by 개발에정착하고싶다 2022. 1. 13.
320x100

와따마... 이거 진짜 강의시간은 1시간도 안되는것같은데 Day 하나씩 끝내는데 집중해도 6시간 이상 걸리는것같네

약간 좀 질척거려지는것같고 진도 빨리 안나가는것같은 점은 싫은데도

분명히 실력은 늘어가고 있고 오히려 이해도가 타 강의에 비해서 높다.

근데 와 진짜 이론과 실전 배합이 적절하다고 볼 수 있다.

아래는 Day2의 노트다.

 

Day-2

 

첫번째

# 🚨 Don't change the code below 👇

two_digit_number = input("Type a two digit number: ")

# 🚨 Don't change the code above 👆

 

####################################

#Write your code below this line 👇

 

first_digit = two_digit_number[0]

second_digit = two_digit_number[1]

 

result = int(first_digit) + int(second_digit)

print(result)

 

 

일때 순서가

two_digit_number = input으로 입력하게 "될" 입력값으로 대체된다.

 

두번째로는

first_digit 이라는 변수에 앞서 input으로 입력한 자리의 0번째 자리 값이 오게되고

second_digit 이라는 변수에도 마찬가지로 input에 입력한 자리의 1번째 자리 값이 오게된다.

이들을 처음에 합치고자 하면 str 처리가 되어서

 

87이라고 입력했을때

87이라는 값을 얻게 된다.

str(8) + str(7)의 수순이기 때문에.

 

하지만 result에서의 int로써 처리해줌으로써 해당 값은 그저 숫자가 된다.

동일하게 87을 입력하게 되었을 경우에 15라는 출력값으로 나온다

 

 

두번째

 

2 **3 은 2의 3승을 의미한다. (제곱)

2 **4는 2의 4승. 이런식.

 

Parentheses 괄호 ()

Exponent 지수 **

Multiplication 곱셈 *

Division 나눗셈 /

Addition 덧셈 +

Subtraction 뺄셈 -

 

곱셈과 나눗셈이 같은 줄에 있으면, 가장 왼쪽에 있는 것 부터 처리된다.

기본 수학 순서와 같되, 곱셈, 나눗셈이 완료된 기준으로도 왼쪽부터 계산된다.

 

 

세번째

 

 

# 🚨 Don't change the code below 👇

height = input("enter your height in m: ")

weight = input("enter your weight in kg: ")

# 🚨 Don't change the code above 👆

 

#Write your code below this line 👇

 

BMI = int(weight) / int(height*2))

 

print(BMI)

 

라고 풀이를 했다.

자꾸 str + str 오류가 떠서

그런데 이건 이거대로 문법오류가 뜬다.

 

  File "main.py", line 8

    BMI = int(weight) / int(height*2))

                                     ^

SyntaxError: unmatched ')'

 

여기에 대해서

weight를 int로 묶어준다. 이것은 정수 그대로이기 때문이다.

하지만 뒤의것은 float 처리 해줘야 한다.

왜냐하면 나누거나 곱한 수는 float속성이기 때문이다.

 

나아가서 **를 박아넣는 자리도 틀렸다. 이건 아직 왜그런지 잘 모르겠다.

 

 

# 🚨 Don't change the code below 👇

height = input("enter your height in m: ")

weight = input("enter your weight in kg: ")

# 🚨 Don't change the code above 👆

 

#Write your code below this line 👇

 

BMI = int(weight) / float(height)**2

 

print(BMI)

 

나아가서 소수점 뒤를 깔끔하게 하는 걸 알려주겠다고 해서

 

# 🚨 Don't change the code below 👇

height = input("enter your height in m: ")

weight = input("enter your weight in kg: ")

# 🚨 Don't change the code above 👆

 

#Write your code below this line 👇

 

BMI = int(weight) / float(height)**2

BMI_as_int = int(BMI)

print(BMI)

 

라는 식으로 BMI를 int로 한번 더 전환해주는 과정도 거쳤다.

그런데 여전히 정답은 0.00026296566837107375

로 나온다 ;; 흠..

뭐가 문제인거야

 

 

네번째

 

정수표현으로써 2.6666을 묶으면 2로 나온다.

즉, 소숫점 뒷자리를 버리는 것이다.

하지만 반올림을 원한다면 round 함수를 써주는 것이다.

그러면 결과값이 3으로 나온다

 

나아가서 round(2.66666, 3)

이라고 하면 0.의 세번째 자릿수까지 끊어서 반올림 되어서

2.667 이렇게 결과 값이 나온다.

 

8%3 같은 경우엔

8을 3으로 나누고 남은 나머지

즉, 2가 출력된다.

그리고 %를 통해서 나오는 숫자는 type을 출력해보면 int이다.

 

만약 int, str, bullin 값을 한 줄에서 표현해주고 싶다면

f-string 을 사용 하면 된다.

예시로 들자면

 

score = 0 #int

height = 1.8 #float

isWinning = True #bull

 

#f-string

print(f"your score is {score}, your height is {height}, you are winning is {isWinning}")

 

 

이렇게 된다.

 

 

다섯번째

 

# 🚨 Don't change the code below 👇

age = input("What is your current age?: ")

# 🚨 Don't change the code above 👆

 

#Write your code below this line 👇

left_year_age = 90-int(age)

left_life_days = left_year_age / 4

four_years_days = 365*3+366

d = left_life_days* four_years_days )

 

라고 해서 마지막에 d라는 변수에 나머지 날자와 나머지 살 날을 4년으로 나눈 값을 곱하려고 하는데

문법 오류가 생긴다.

 

참 신기하게도

 

print 처리를 하면 동작을 한다.

 

# 🚨 Don't change the code below 👇

age = input("What is your current age?: ")

# 🚨 Don't change the code above 👆

 

#Write your code below this line 👇

left_year_age = 90-int(age)

left_life_days = left_year_age / 4

four_years_days = 365*3+366

print(left_life_days* four_years_days )

 

이렇게.

 

뭔가 변수처리하는데 왜 이런 오류가 생기는 걸까?

 

음.. 유사 이름의 중복성은 사용이 되질 않는걸까.

result 로 변수 이름을 변경해주니 작동한다.

 

# 🚨 Don't change the code below 👇

age = input("What is your current age?: ")

# 🚨 Don't change the code above 👆

 

#Write your code below this line 👇

left_year_age = 90-int(age)

left_life_days = left_year_age / 4

four_years_days = 365*3+366

result = int(left_life_days* four_years_days )

 

print(f"left my life is change to days {result} days left")

 

이렇게 하니 나의 살 날이 몇날 남았는지 알게된다.

 

이정도 까지 하니 나머지를 몇주로, 몇달로 까지 해주는 건 쉬었다.

 

# 🚨 Don't change the code below 👇

age = input("What is your current age?: ")

# 🚨 Don't change the code above 👆

 

#Write your code below this line 👇

left_year_age = 90-int(age)

left_life_days = left_year_age / 4

four_years_days = 365*3+366

result_days = int(left_life_days* four_years_days )

result_weeks = int(result_days/7)

result_month = int(left_year_age*12)

 

print(f"you have {result_days} days, {result_weeks} weeks and {result_month} months left.")

 

일단 정답 판은

 

# 🚨 Don't change the code below 👇

age = input("What is your current age?: ")

# 🚨 Don't change the code above 👆

 

#Write your code below this line 👇

 

age_as_int = int(age)

years_remaining = 90 - age_as_int

days_remaining = years_remaining * 365

weeks_remaining = years_remaining * 52

months_remaining = years_remaining * 12

 

message = f"you have {days_remaining} days, {weeks_remaining} weeks, and {months_remaining} months left."

 

print(message)

 

 

이러하다.

정말 간결하고 알아보기도 쉽다.

다음에도 하게되고 또 하게 되면서 좀 더 간결하게 생각 할 수 있도록 해보자.

 

 

여섯번째

 

팁 계산기

 

Total_pay = int(input("What was the total bull?: $"))

Tip = int(input("What percentage tip would you like to give?: %"))

Split_people = int(input("How many people to split the bill?: $"))

Each_pay_price = int(Total_pay/Split_people) + int((Total_pay/Split_people)*Tip*0.01)

 

print(f"Each people need pay ${Each_pay_price} Okay?")

 

내가 만든게 이거고

 

중간 과정에서 str와 str로 결과값이 충돌했다. 이를 방지하기 위해서 int를 깔아 주었고

int를 깔아주었더니 다음은 float 로 결과값 도출이 되어서 int를 좀 더 박아줬고

그 다음은 결과값이 오히려 나누기 전 보다 더 나오더라. 그래서 생각하다가 백분율을 어떻게 표현해줄까 하다가 팁옆에 0.01을 붙여줌

 

정석으로 해주신건

 

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 = "{:2f}".format(bill_per_person)

print(f"Each person should pay: ${final_amount}")

 

이것이다.

결과값의 과정에서 납득이 안되는것은 왜 소수접 2째자리까지 끊는다고 선언했는데

하염없이 소숫점 뒷자리가 나오는지는 모르겠다.

300x250