Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
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
Tags
more
Archives
Today
Total
관리 메뉴

JINIers

[Python 100 day challenge ] Day 3 - 1 본문

공부/Python

[Python 100 day challenge ] Day 3 - 1

JINIers 2024. 3. 15. 10:56

day3은 제어흐름 및 논리연산자를 배웠다.

안젤라는 day 첫 강의에 'n일차 목표 : ____ 만들기'

이렇게 알려주는데

어려운 것도 있고 쉬운 것도 있음

어려우면 다시 그 회차를 복습하고 다시 작성했다.

 


# if / else 및 조건 연산자를 이용한 제어흐름

동영상 보면서 한거라 종종 내가 봐도 이건 뭘까..싶은 것도 있음 ㅜㅜㅋㅋㅋ

# # 욕조 예시
water_level = int(input("water_level: "))
if water_level > 80 :
  print("Drain water")
else:
  print("continue")



print("Welcome to the rollercoaster!")
height = int(input("What is your height in cm? "))

if height >= 120:
    print("You can ride the rollercoaster!")
    age = int(input("What is your age? "))
    if age < 12:
        print("Please pay $5.")
    elif age <= 18:
        print("Please pay $7.")
    elif age <= 23:
        print("Please pay $10.")
    else:
        print("Please pay $12.")
else:
    print("Sorry, you have to grow taller before you can ride.")

# # 부등호
# >
# <
# >=
# <=
# ==
# !=

# 홀수 짝수(ODD or Even)

if / else 사용하기

# Which number do you want to check?
number = int(input())

value = number % 2 

if value == 0 :
  print("This is an even number.")
else :
  print("This is an odd number.")

 

실행결과

- 짝수일 때

- 홀수일 때

 


# BMI 2.0

중첩 if / elif문 이용하기

if /elif는 흐름도가 꼭 필요하다. 안그러면 어디서 꼬였는지 알 수 없기때문에 흐름도를 보고 작성한다.

 

사용자의 체중과 키를 기반으로 체질량 지수(BMI)를 해석하는 프로그램 작성하

BMI 값▼
18.5 미만:  저체중
18.5 이상 25 미만: 정상 체중
25 이상 30 미만: 약간 과체중
30세 이상 35세 미만: 비만
35세 이상: 비만

# Enter your height in meters e.g., 1.55
height = float(input("height: "))
# Enter your weight in kilograms e.g., 72
weight = int(input("weight: "))


bmi = weight / (height ** 2)

if bmi < 18.5:
  print(f"Your BMI is {bmi}, you are underweight.")
elif 18.5 <= bmi < 25:
  print(f"Your BMI is {bmi}, you have a normal weight.")
elif 25 <= bmi < 30:
  print(f"Your BMI is {bmi}, you are slightly overweight.")
elif 30 <= bmi < 35:
  print(f"Your BMI is {bmi}, you are obese.")
else:
  print(f"Your BMI is {bmi}, you are clinically obese.")

 

실행결과

 


# leap year(윤년) / dificult challenge

특정연도가 윤년인지 확인하는 프로그램 작성

윤년인지 확인하는 방법

  • 나머지 없이 4로 나누어지는 매년
  • 단, 나머지 없이 100으로 균등하게 나누어지는 매년은 제외됩니다.
  • 연도가 나머지 없이 400으로 나누어지는 경우는 제외

윤년 계산이 어려웠다ㅜ

year = int(input("year: "))

if (year % 4) == 0:
  if (year % 100) == 0:
    if (year % 400) == 0:
      print("Leap year")
      
    else:
      print("Not leap year")
  else:
    print("Leap year")
else:
  print("Not leap year")

 

실행결과

- 윤년이 아닐 때

- 윤년일 때

 

 

'공부 > Python' 카테고리의 다른 글

[Python 100 day challenge ] Day 4  (0) 2024.03.27
[Python 100 day challenge ] Day 3 - 2  (1) 2024.03.15
[Python 100 day challenge ] Day 2  (1) 2024.03.15
[Python 100 day challenge ] Day 1  (0) 2024.03.15
1장 파이썬 기초 연습문제  (0) 2022.06.11
Comments