๐Ÿ–ฅ๏ธ[Python] 5-1. ๋ฐ˜๋ณต๋ฌธ

thisk336ยท2023๋…„ 5์›” 15์ผ
0

Python

๋ชฉ๋ก ๋ณด๊ธฐ
6/17
post-thumbnail

while statement

  • while๋ฌธ์€ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•  ๋•Œ ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค.
  • while (์กฐ๊ฑด):
    (statement1) ...
    (statement2) ...
    (statement3) ...
  • ์กฐ๊ฑด์ด ๋งŒ์กฑํ•˜๋Š” ๋™์•ˆ(while) statement1, 2, 3์„ ๋ฐ˜๋ณตํ•œ๋‹ค.
# while ๋ฌธ์„ ์‚ฌ์šฉํ•œ 2๋‹จ ๊ตฌํ˜„
i = 1
while i <= 9 :
    print(f"2 x {i} = {2*i}")
    i = i + 1

์‹ค์ œ ์žํŒ๊ธฐ ๊ธฐ๊ณ„๋Š” ๋ˆ์„ ํ•œ๋ฒˆ ๋„ฃ๊ณ  ์ข…๋ฃŒ๋˜์ง€ ์•Š๋Š”๋‹ค. ์žํŒ๊ธฐ ๊ธฐ๊ณ„(ํ”„๋กœ๊ทธ๋žจ)์ด ์ข…๋ฃŒ๋˜๋Š” ๊ฒฝ์šฐ๋Š” ์ปคํ”ผ๊ฐ€ ๋‹ค ๋–จ์–ด์กŒ์„ ๋•Œ์ด๋‹ค.

Q. ์ปคํ”ผ๊ฐ€ ๋‹ค ๋–จ์–ด์งˆ ๋•Œ๊นŒ์ง€ ์žํŒ๊ธฐ ํ”„๋กœ๊ทธ๋žจ์„ ๋ฐ˜๋ณตํ•˜๋ฉด์„œ ์‹คํ–‰ํ•œ ๋’ค, ๋ˆ์„ ์ž…๋ ฅ๋ฐ›์•„์„œ ์ปคํ”ผ๋ฅผ ์ฃผ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ด๋ณด์ž.

# ์žํŒ๊ธฐ์˜ ์ปคํ”ผ ์ˆ˜๋Ÿ‰
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 -= 1
        print("%d์›์„ ๋Œ๋ ค์ค๋‹ˆ๋‹ค." % (money-300))
        
    # ์ปคํ”ผ๊ฐ€ ๋‹ค ๋–จ์–ด์ง„ ๊ฒฝ์šฐ ์•Œ๋ ค์•ผํ•œ๋‹ค.
print("์ปคํ”ผ๊ฐ€ ๋ชจ๋‘ ์†Œ์ง„๋˜์—ˆ์œผ๋‹ˆ, ๊ด€๋ฆฌ์ž์—๊ฒŒ ๋ฌธ์˜ํ•ด์ฃผ์„ธ์š”.")

๋ฐ˜๋ณต๋ฌธ์„ ์ œ์–ดํ•˜๋Š” break, continue

  • ๋งŒ์•ฝ ๋ฐ˜๋ณต๋ฌธ์„ ์ˆ˜ํ–‰ํ•˜๋‹ค๊ฐ€ ๋” ์ด์ƒ ๋ฐ˜๋ณต์ด ํ•„์š”์—†๋Š” ๊ฒฝ์šฐ์—๋Š” ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ?

break statement

  • ๋งŒ์•ฝ ๋ฐ˜๋ณต๋ฌธ์„ ์ˆ˜ํ–‰ํ•˜๋‹ค๊ฐ€ ํŠน์ • ์กฐ๊ฑด์—๋งŒ ๊ฑด๋„ˆ๋›ฐ๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ์—๋Š” ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ?

continue statement

RE : ์‹ค์ œ ์žํŒ๊ธฐ ๊ธฐ๊ณ„๋Š” ๋ˆ์„ ํ•œ๋ฒˆ ๋„ฃ๊ณ  ์ข…๋ฃŒ๋˜์ง€ ์•Š๋Š”๋‹ค. ์žํŒ๊ธฐ ๊ธฐ๊ณ„(ํ”„๋กœ๊ทธ๋žจ)์ด ์ข…๋ฃŒ๋˜๋Š” ๊ฒฝ์šฐ๋Š” ์ปคํ”ผ๊ฐ€ ๋‹ค ๋–จ์–ด์กŒ์„ ๋•Œ์ด๋‹ค.

Q. ๋งŒ์•ฝ์— ์‚ฌ์šฉ์ž๊ฐ€ ๋ˆ์ด ์•„๋‹Œ ๋‹ค๋ฅธ๊ฑธ ๋„ฃ์—ˆ์„ ๋•Œ๋Š” ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ? ์ œ๋Œ€๋กœ ๋œ ์ˆซ์ž ์ž…๋ ฅ์„ ๋ฐ›์„ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•ด์„œ ์š”์ฒญ์„ ํ•˜๋ ค๋ฉด?

# ์žํŒ๊ธฐ์˜ ์ปคํ”ผ ์ˆ˜๋Ÿ‰
coffee = 5

while coffee > 0:

    # ์‹ค์ œ๋กœ๋Š” ์žํŒ๊ธฐ๋ฅผ ํ†ตํ•ด์„œ ๋„ฃ์€ ๊ธˆ์•ก.
    try :
        money = int(input("๊ธˆ์•ก์„ ์ž…๋ ฅํ•˜์„ธ์š”."))
    except :
        print("๋ˆ์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.")
        continue
    
    if money == 300:
        # ์‹ค์ œ๋กœ ์ด ํŒŒํŠธ๋Š” ์žํŒ๊ธฐ์—์„œ ์ปคํ”ผ๋ฅผ ๋ฝ‘๋Š” ๋ช…๋ น์œผ๋กœ ๋Œ€์ฒด๋œ๋‹ค.
        print("Coffee")
        # ์ด์ œ ์ปคํ”ผ๋ฅผ ํ•˜๋‚˜์”ฉ ์ค„์ธ๋‹ค.
        coffee = coffee - 1

    elif money < 300:
        # ์‹ค์ œ๋กœ ์ด ํŒŒํŠธ๋Š” ๋ˆ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
        print("%d์›์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค." % money)
     
    else: # or elif money > 300:
        print("Coffee")
        # ์ปคํ”ผ๋ฅผ ๋ฝ‘์•„์ฃผ๊ณ 
        coffee = coffee - 1
        # ์ด์ œ ์ปคํ”ผ๋ฅผ ํ•˜๋‚˜์”ฉ ์ค„์ธ๋‹ค.
        print("%d์›์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค." % (money-300))
        # ๊ฑฐ์Šค๋ฆ„๋ˆ์„ ๋Œ๋ ค์ค€๋‹ค.
        
        
    # ์ปคํ”ผ๊ฐ€ ๋‹ค ๋–จ์–ด์ง„ ๊ฒฝ์šฐ ์•Œ๋ ค์•ผํ•œ๋‹ค.
print("์ปคํ”ผ๊ฐ€ ๋ชจ๋‘ ์†Œ์ง„๋˜์—ˆ์œผ๋‹ˆ, ๊ด€๋ฆฌ์ž์—๊ฒŒ ๋ฌธ์˜ํ•ด์ฃผ์„ธ์š”.")

0๊ฐœ์˜ ๋Œ“๊ธ€