money = 600
if money == 300:
print("Coffee")
elif money > 300:
print("Coffee")
print("%d원을 돌려줍니다." % (money - 300))
else:
print("%d원을 돌려줍니다." % money)
money = int(input("금액을 넣어주세요. : "))
if money == 300:
print("Coffee")
else:
if money > 300:
print("Coffee")
print("%d원을 돌려줍니다." % (money - 300))
else:
print("%d원을 돌려줍니다." % money)
dan = 2
while dan < 10:
number = 1
while number < 10:
print ("%d x %d = %d" % (dan, number, dan*number))
number = number +1
dan = dan +1
coffee = 5
while coffee > 0:
money = int(input("금액을 입력해주세요 : "))
if money == 300:
print("Coffee")
coffee = coffee - 1
elif money < 300:
print("%d원을 돌려줍니다." % money)
else:
print("Coffee")
coffee = coffee - 1
print("%d원을 돌려줍니다." % (money - 300))
print("커피가 모두 소진되었습니다. 관리자에게 문의해주세요.")
for i in range(1, 11):
print(i)
coffees = ['아메리카노', '카페라떼', '카페모카', '바닐라라떼', '핸드드립', '콜드브루']
prices = [4100, 4600, 4600, 5100, 6000, 5000]
for i in range(len(coffees)):
if prices[i] <= 5000:
print(coffees[i])
결과물
아메리카노
카페라떼
카페모카
콜드브루
위 함수에서 range함수를 사용하여서 i가 0부터 시작하는 것이 아닌지에 대해 의문이 생길 수 있지만, len함수를 사용하였기에 1부터 시작하게 된다.
실행하면 i값이 더해지는 것이 아니라, range함수가 증가하게 된다.
반복문을 제어하는 용도
돈이 모자랄 경우 추가 안내를 하는 커피 자판기
coffee = 5
extra_money = 0
while coffee > 0:
money = int(input("금액을 입력하세요."))
money = money + extra_money
if money == 300:
print("Coffee")
coffee = coffee - 1
elif money < 300:
print("돈이 모자랍니다. 추가로 금액을 입력해주세요.")
extra_money = money
continue
else:
print("Coffee")
coffee = coffee - 1
print("%d원을 반환합니다." % (money-300))
extra_money = 0
print("커피가 모두 소진되었습니다. 관리자에게 문의해주세요.")
i = 10
star = '*'
for j in range(i , 0, -1):
print(j * star)
money = int(input("돈을 넣어주세요. : "))
coffee, cocoa, yulmu = [int(x) for x in input("뽑고 싶은 커피, 코코아, 율무차의 수량을 입력해주세요. 쉼표를 기준으로 순서대로 입력하시면 됩니다. : ").split(',')]
print(coffee)
print(f"거스름돈은 {money - 300*coffee - 200*cocoa - 300*yulmu}원 입니다. 남은 커피는 {5-coffee}잔, 코코아는 {5-cocoa}잔, 율무차는 {5-yulmu}잔 입니다.")
print([x for x in range(1, 101) if x % 5 == 0])
def add(a, b):
c = a + b
return c
add (10, 11)
def change_name(a):
# local variable : 지역 변수
name = "lee" # local
print("in function :", a)
return name
name = "kim" #global
change_name(name)
출력 결과물
in function : kim
'lee'
지역 변수와 전역 변수가 다르기 때문에 사용할 때 유의하여야 한다.
def full_name(a, b):
# local variable : 지역 변수
c = a + b # local
print("My name is :", c)
# return name
last = "hong"
first = "gildong"
full_name(last, first)