학습목표:
참고자료: https://replit.com/@appbrewery/day-3-1-exercise
/ 키-나이 다이어그램 / 키-나이2 다이어그램
1. 콘솔에 입력한 숫자가 홀수 or 짝수 인지 확인시켜주는 프로그램을 짜시오
나머지를 구하는 연산자: %
숫자를 다른 숫자로 나누고 나머지를 구하는 연산자
print(7 % 3) # 실행결과: 1
print(10 % 5) # 실행결과: 2
Write a program that works out whether if a given number is an odd or even number.
Even numbers can be divided by 2 with no remainder.
e.g. 86 is even because 86 ÷ 2 = 43
43 does not have any decimal places. Therefore the division is clean.
e.g. 59 is odd because 59 ÷ 2 = 29.5
29.5 is not a whole number, it has decimal places. Therefore there is a remainder of 0.5, so the division is not clean.
The modulo is written as a percentage sign (%) in Python. It gives you the remainder after a division.
# 6 ÷ 2 = 3 with no remainder.
6 % 2 = 0
# 5 ÷ 2 = 2 x 2 + 1, remainder is 1.
5 % 2 = 1
# 14 ÷ 4 = 3 x 4 + 2, remainder is 2.
14 % 4 = 2
Example Input 1
43
Example output 1
This is an odd number.
Example Input 2
94
Example Output 2
This is an even number.
(number 를 2로 나눈 나머지값이 0인지 아닌지 T/F판별하는 문제므로, == 0 으로 나타내는것)
# 🚨 Don't change the code below 👇
number = int(input("Which number do you want to check? "))
# 🚨 Don't change the code above 👆
#Write your code below this line 👇
if number % 2 == 0:
print("This is an even number.")
else :
print("This is an odd number.")
일반 조건문은 앞선 조건이 참이면 do this, 참이 아니면 else do that 을 실행한다
중첩 조건문은 첫째 조건이 통과돼야만, 다음 조건을 확인할 수 있음
중첩 조건문은 조건문 if구문 안에 조건문 if~else까지 들어가는 구문임.
중첩 조건문에 첫번째 if condition 조건을 충족(True)하고, 다음 if another condition 을 충족해야 do this 를 실행함
첫번째 if condition 조건을 충족했지만, 다음 if another condition 조건을 충족 못했을시, else의 do that 을 실행함
. 조건 3개 이상일때 쓰는 elif 조건문
elif 조건문은 원하는만큼 추가 가능함
elif 조건문~ 뜻: condition 2 를 충족한다면 do B를 실행한다
condition 1 를 충족하지 않는다면, condition 2는 충족하는지 봐줄래? 라는 뜻
# 일반 조건문if~else
if condition :
do this
else :
do that
# 중첩 조건문
if condition :
if another condition :
do this
else :
do that
else :
do that
if condition1 :
do A
elif condition2 :
do B
else :
do this
1)
age 변수를 선언 안해줘서 오류뜸
< >= <= != 부등호 연산자는 같은 타입끼리 적용할 수 있는데, height 랑 age를 숫자형(float)으로 형변환해야함
다이어그램 상에 120 이거나 120 넘으면 탑승가능하므로 >= 부등호로 표기해야함
height = int(input("What is your height?"))
if height >= 120 :
if age > 18 :
print("you can ride. \nprice : $12")
else :
print("$7")
else :
print("you can't ride")
height = float(input("What is your height?"))
age = int(input("What is your age?"))
if height >= 120 :
if age > 18 :
print("you can ride. \nprice : $12")
else :
print("$7")
else :
print("you can't ride")
height = float(input("What is your height?"))
age = int(input("What is your age?"))
if height >= 120 :
print("you can ride")
if age > 18 :
print("please pay $12")
elif age >=12 :
print("please pay $7")
else :
print("please pay $5")
else :
print("you can't ride")
#유데미 #유데미코리아 #스타트위드유데미 #스터디윗미