well-balanced

[python] 미니 프로그램 만들기 (달에서의 몸무게) 본문

Python

[python] 미니 프로그램 만들기 (달에서의 몸무게)

Cosmian 2018. 12. 25. 20:40

PYTHON 미니 프로그램 만들기


import sys 

시스템 모듈 실행

def moon_weight(): 

moon_weight 함수 생성

print('please enter your current earth weight') 

지구에서의 몸무게를 입력하라는 메세지 출력

weight=float(sys.stdin.readline()) 

시스템 모듈의 sys.stdin.readline을 이용해 값을 입력할 수 있도록 하고, float 함수를 이용해 readline에 반환된 문자열을 실수형으로 바꿔준다. 그 후 값을 weight에 저장

print('please enter the amount your weight might increase each year')

연마다 늘어날 것으로 예상되는 몸무게 값을 입력하라는 메세지 출력

increase=float(sys.stdin.readline())

값을 입력할 수 있도록 하고, 값을 increase에 저장

print('please enter the number of years')

연수를 입력하라는 메세지 출력

years=int(sys.stdin.readline())

문자열을 숫자로 바꾸도록 int 함수 사용(년수는 정수형으로 나올 것이기에 float가 아닌 int 사용)하고 값을 years에 저장

for year in range(1,years+1): 

year(년수)가 1부터 years에 입력된 값의 +1한 값만큼 반복하도록 for 루프 이용

weight=weight+increase 

매년 증가할 값만큼 for 루프를 반복할 때마다 몸무게 업데이트

moon_weight=weight*0.165 

새로 업데이트된 몸무게에 0.165를 곱하여 달에서의 몸무게를 규정

print('%s years is %s' % (year, moon_weight))

%s(반복될 year 값) years is %s(달에서의 몸무게) 메세지 출력

moon_weight()

새로 생성한 moon_weight 함수 호출





Comments