Study/Python

[Python] 조건문

YoungD 2023. 10. 5. 17:14
  • 조건문 

 

In [1]:
if True : 
    print('실행문 1')
    print('실행문 2')
    if False :
        print('실행문 3')
        print('실행문 4')
print('조건문 종료')
# 들여쓰기 위치 중요!

 

실행문 1
실행문 2
조건문 종료

 

  • 조건문 elif 

 

실습

 

In [2]:
score = int(input('점수 입력 >> '))
if 90 <= score <=100 :
    print('{}점은 A학점 입니다.'.formant(score))
elif score >= 80:
    print('{}점은 B학점 입니다.'.format(score))
elif score >= 70:
    print('{}점은 C학점 입니다.'.format(score))
elif score >= 60:
    print('{}점은 D학점 입니다.'.format(score))
elif score < 60:
    print('{}점은 F학점 입니다.'.format(score))

 

점수 입력 >> 70
70점은 C학점 입니다.

 

실습

 

In [3]:
print('-----[SM Soffee]-----')
inputMoney = int(input('금액을 입력하세요 >>'))
print('커피를 고르세요.')
menu = int(input('[1]에스프레소(600원) [2]아메리카노(1000원) [3]카페라떼(1300원) >>'))

if menu == 1 :
    price = 600
elif menu == 2 :
    price = 1000
elif menu == 3 :
    price = 1300
 
# ==
# if menu == 1 :
#         print(f'거스름돈 : {inputMoney - 600}원')
# elif menu == 2:
#         print(f'거스름돈 : {inputMoney - 1000}원')
# elif menu == 3:
#         print(f'거스름돈 : {inputMoney - 1300}원')

if inputMoney < price :
        print('돈이 부족해요');
        price = 0
        
print(f'거스름돈 : {inputMoney - price}원')

 

-----[SM Soffee]-----
금액을 입력하세요 >>2000
커피를 고르세요.
[1]에스프레소(600원) [2]아메리카노(1000원) [3]카페라떼(1300원) >>3
거스름돈 : 700원