Control Statement 에서의 조건문
특정 조건에 따라 Boolean 값으로 받아 다음 작업에 대한 판단을 한다.
만약 "조건" 이면 무엇을 해라.
if 조건 :
tab
a = 5
# 만약 a 가 5 이면 다음 작업을 하여라
if a == 5:
print(True)
a = 7
if a == 5:
print(True)
# 이경우 if에서 조건을 충족 못함으로 아무런 작업도 실행이 안된다.
money = int(input("Input ypur money: "))
if money == 300:
print("Here is your Coffee")
money = int(input("Input ypur money: "))
if money > 300:
print("Here is your Coffee and your change %d" %(money-300))
money = int(input("Input ypur money: "))
if money < 300:
print("Not enough money. Here is your change %d" %(money))
money = int(input("Input ypur money: "))
if money == 300:
print("Here is your Coffee")
elif money > 300:
print("Here is your Coffee and your change %d" %(money-300))
else :
print("Not enough money. Here is your change %d" %(money))
money = int(input("Input Your Money"))
if money >= 300:
print("Here is your Coffee")
if money > 300:
print("Here is your change %d." %(money-300))
else :
print("Not enough money. Here is your change %d." %(money))
프로그램에서 가장 중요한 반복이다.
컴퓨터의 작업은 반복에 최적화 되어있다.
파이썬에서는 while, for 2가지의 statement를 제공한다.
# 구구단 2단: 입력된 숫자부터 9까지 2단 실행
N = int(input("Input your number"))
while N < 10:
print("2 X %d = %d" % (N, 2*N))
N = N+1
# 팔 수 있는 커피가 5잔 있다. 이때 커피가 다 팔릴 때까지 코드 실행.
coffees = 5
while coffees > 0:
money = int(input("Input your money: "))
if money == 300:
print("Here is your Coffee.")
coffees = coffees -1
elif money < 300:
print("Not enough money. Here is your change %d" % (money))
else:
print("Here is your Coffee and your change %d" % (money-300))
coffees = coffees - 1
if coffees == 0:
print("Sorry. Sold Out.")
L = [1, 2, 3, 4, 5, 6]
for i in L:
print(i)
coffees = ['아메리카노', '카페라떼', '카페모카', '바닐라라떼', '핸드드립', '콜드브루']
for coffee in coffees: ## 단수 in 복수형 으로 작성해도 괜찮다.
print(coffee)
# range 함수를 이용해 1부터 4까지 출력하는 프로그램을 작성해보자. for문을 이용해서!
range(3) ## 0, 1, 2 !!This is not a list!!
range(1, 5) ## 1, 2, 3, 4
range(2, 10, 2) ## 2 이상 부터 10 미만까지 2씩 올린다.
L = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for i in range(len(L)):
print(L[i])
coffees = ['아메리카노', '카페라떼', '카페모카', '바닐라라떼', '핸드드립', '콜드브루']
prices = [4100, 4600, 4600, 5100, 6000, 5000]
for i in range(len(coffees)): ## index 0, 1, 2, 3, 4, 5 를 불러온다.
if prices[i] <= 5000:
print(coffees[i])
print("------")
# enumerate
for i, price in enumerate(prices):
if price <= 5000:
print(coffees[i])
print("------")
# zip ## 두개의 리스트가 길이가 같아야 한다.
for coffee, price in zip(coffees, prices):
if price <= 5000:
print(coffee)
coffees = ['아메리카노', '카페라떼', '카페모카', '바닐라라떼', '핸드드립', '콜드브루']
prices = [4100, 4600, 4600, 5100, 6000, 5000]
for i in range(len(coffees)):
if prices[i]-500 <= 5000:
print(coffees[i])
break statement
continue statement
coffees = 5
while True: ## 이경우 무한 루프가 된다.
money = int(input("Input your money: "))
if money == 300:
print("Here is your Coffee")
coffees = coffees - 1
elif money < 300:
print("Here is your change %d" % (money))
else:
print("Here is your Coffee and change %d" % (money-300))
coffees = coffees - 1
if coffees == 0:
print("Sold out")
break ##위에 무한 루프인 while True를 break 한다.
coffee = 5
extra_money = 0
while True:
money = int(input("Input your money: "))
money = money + extra_money ## 머니를 업데이트 해준다.
if money == 300:
print("Here is your Coffee")
coffee = coffee - 1
elif money < 300:
print("Not enough money. Please input more money if you want a Coffee")
extra_money = money ## 여기서 업데이트된 extra_money 가 위에 새롭게 들어온 돈에 업데이트 된다.
continue ## while 로 돌아 간다.
elif money > 300:
print("Here is your Coffee and change %d" % (money-300))
coffee = coffee - 1
if coffee == 0:
print("Sold out. Sorry for your inconvenience.")
break
##정산
extra_money = 0 ## 위에 elif < 300 에서
## extra_money에 지정된 값이 있음으로 돈을 0으로 해주어야 한다.