[Python] 3-2장 연산자 응용(초 단위 변환, 화씨 섭씨, 거스름돈, 복리 등)

- 초 단위로 입력 받아서 몇 분 몇 초인지 계산

second = int(input("초를 입력하시오: "))
minute = second // 60
second %= 60
print(minute,"분", second, "초")

 

 

- 가게 매출 계산

americano = int(input("아메리카노 판매 개수: "))
cafelatte = int(input("카페라떼 판매 개수: "))
capucino = int(input("카푸치노 판매 개수: "))

sales = americano * 2000 + cafelatte * 3000 + capucino * 3500
print("총 매출은", sales,"입니다.")

margin = sales - 100000
if margin > 0:
    print("흑자입니다.")
elif margin == 0:
    print("이익도 손실도 없습니다.")
else:
    print("적자입니다,")

 

 

- 화씨 온도 -> 섭씨 온도로 변환 (반대로 섭씨 -> 화씨 변환)

# 화씨 온도 -> 섭씨 온도
ftemp = int(input("화씨온도: "))
ctemp = (ftemp - 32) * 5 / 9
print("섭씨온도:", ctemp)

# 섭씨 온도 -> 화씨 온도
ctemp = int(input("섭씨온도: "))
ftemp = ctemp * 9 / 5 + 32
print("화씨온도:", ftemp)

 

 

- BMI 계산

weight = float(input("몸무게를 kg 단위로 입력하시오: "))
height = float(input("키를 미터 단위로 입력하시오: "))
bmi = weight / (height**2)
print("당신의 BMI =", bmi)

 

 

- 자동판매기 거스름

money = int(input("투입한 돈: "))
price = int(input("물건 값: "))

change = money - price
print("거스름돈:", change)

coin500 = change // 500
change %= 500
coin100 = change // 100
change %= 100
coin50 = change // 50
change %= 50
coin10 = change // 10

print("500원 동전의 개수:", coin500)
print("100원 동전의 개수:", coin100)
print("50원 동전의 개수:", coin50)
print("10원 동전의 개수:", coin10)

 

 

- 원리금 합계 복리로 계산

money = int(input("원금을 입력하시오: "))
r = float(input("이자율을 입력하시오: "))
n = int(input("몇 년 후로 하시겠습니까? "))
result = (money*(1+r)**n)
print("원리금 합계는", result)

 

 

- 평균 구하기

x = int(input("첫 번째 수를 입력하시오: "))
y = int(input("두 번째 수를 입력하시오: "))
z = int(input("세 번째 수를 입력하시오: "))
avg = (x + y + z) / 3
print("평균:", avg)

 

 

- 두 수 입력 받아 연산 결과 출력 (max(x,y), min(x,y) 함수 사용)

x = int(input("x: "))
y = int(input("y: "))
print("두 수의 합:", x+y)
print("두 수의 차:", x-y)
print("두 수의 곱:", x*y)
print("두 수의 평균:", (x+y)/2)
print("큰 수:", max(x,y))
print("작은 수:", min(x,y))

 

 

- 두 점 사이의 거리 계산

x1 = int(input("x1: "))
y1 = int(input("y1: "))
x2 = int(input("x2: "))
y2 = int(input("y2: "))
distance = ((x1 - x2)**2 + (y1 - y2)**2)**0.5
print("두 점 사이의 거리=", distance)

 

 

- 두 점 연결하는 선 그리기 & 직선 길이 계산

import turtle
t = turtle.Turtle()
t.shape("turtle")
x1 = int(input("x1: "))
y1 = int(input("y1: "))
x2 = int(input("x2: "))
y2 = int(input("y2: "))
t.up()
t.goto(x1, y1)
t.down()
t.goto(x2, y2)
distance = ((x1 - x2)**2 + (y1 - y2)**2)**0.5
t.write("두 점 사이의 거리=", distance)
t.home()
t.clear()

 

 

- 움직이는 물체의 운동에너지

weight = int(input("물체의 무게를 입력하시오(킬로그램): "))
speed = int(input("물체의 속도를 입력하시오(미터/초): "))
energy = 1/2 * weight * speed ** 2
print("물체는",energy,"(줄)의 에너지를 가지고 있다.")

 

 

※ 두근두근 파이썬 3장 연습문제 참조