๐Ÿ“„ Python

5. while ๋ฐ˜๋ณต๋ฌธ

sa1t 2022. 6. 9. 10:38
๋ฐ˜์‘ํ˜•
# while ๋ฐ˜๋ณต๋ฌธ
# for => ํšŸ์ˆ˜
# while => ์กฐ๊ฑด์‹์„ ๋งŒ๋“ค์–ด ์กฐ๊ฑด์ด ์„ฑ๋ฆฝ๋  ๋•Œ ๊นŒ์ง€ ๋ฐ˜๋ณต๋ฌธ์„ ์‹คํ–‰
"""
ํ˜•์‹

while ๋ณ€์ˆ˜ < ๋ ๊ฐ’ (๋ฐ˜๋ณต๋ฌธ์ด ๋๋‚  ์กฐ๊ฑด) :
    ์‹คํ–‰๋ฌธ
    ๋ณ€์ˆ˜ = ๋ณ€์ˆ˜ + 1
"""

i=0
while i<3 :
    print("%d ๊ฐ’ : while๋ฌธ ์‹คํ–‰ ์ค‘" % i)
    i=i+1
print("๋")

# while : ์กฐ๊ฑด์‹์ด ๋งŒ์กฑ๋˜์ง€ ์•Š๋Š” ์กฐ๊ฑด์„ ์ง€์ •ํ•˜๋ฉด
# ๋ฌดํ•œ๋ฃจํ”„ => ์šฐ๋ฆฌ๊ฐ€ ์ด์šฉ
# I/O : input()

# 1~10๊นŒ์ง€์˜ ํ•ฉ
sum = 0
i = 1

while i<11:
    sum=sum+i
    i=i+1

print("1๋ถ€ํ„ฐ 10๊นŒ์ง€์˜ ํ•ฉ : %d" %sum)

# Quiz) 1~100๊นŒ์ง€ ์ง์ˆ˜์˜ ํ•ฉ
sum=0
i=0
while i<101:
    sum=sum+i
    i=i+2

print("1๋ถ€ํ„ฐ 100๊นŒ์ง€ ์ง์ˆ˜์˜ ํ•ฉ : %d" %sum)


# Quiz) ์‹œ์ž‘๊ฐ’, ๋๊ฐ’, ์ฆ๊ฐ€๊ฐ’ ์ž…๋ ฅ๋ฐ›์•„ ํ•ฉ ๊ตฌํ•˜๊ธฐ
a = int(input("์‹œ์ž‘ ๊ฐ’์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š” : "))
b = int(input("๋ ๊ฐ’์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š” : "))
c = int(input("์ฆ๊ฐ€ ๊ฐ’์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š” : "))
sum = 0
i=0

while a<b+1:
    sum=sum+a
    a=a+c

print("์ดํ•ฉ์€ : %d ์ž…๋‹ˆ๋‹ค" %sum)


#### ๋ฌดํ•œ ๋ฐ˜๋ณต ๋Œ๋ฆฌ๊ธฐ (๋ฌดํ•œ ๋ฃจํ”„) ####
while True :
    print("ใ…‹", end="")


# ์™„์„ฑ๋˜์ง€ ์•Š์„ ์กฐ๊ฑด๋ฌธ์„ ์ž‘์„ฑํ•˜๋ฉด ๋ฌดํ•œ๋ฃจํ”„์— ๋น ์ง
# ์‚ฌ์šฉ์ž๊ฐ€ ์ข…๋ฃŒํ•  ๋•Œ ๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜๊ฒŒ ์‚ฌ์šฉ
# ctrl + c : ์ธํ„ฐ๋ŸฝํŠธ (์ข…๋ฃŒ)

sum = 0
a,b=0,0

while True :
    a= int(input("์ฒซ๋ฒˆ์งธ ์ˆซ์ž : "))
    b= int(input("๋‘๋ฒˆ์งธ ์ˆซ์ž : "))
    sum = a+b
    print("%d + %d = %d" %(a,b,sum))



# Quiz ) ์‚ฌ์น™์—ฐ์‚ฐ ๊ณ„์‚ฐ๊ธฐ op ( + = * / )

a,b,op = 0,0,0

while True :
    a = int(input("์ฒซ๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : "))
    op = input("์—ฐ์‚ฐ์ž (+,-,*,/) : ")
    b = int(input("๋‘๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : "))

    if op== "+" :
        print("%d + %d = %d " % (a,b,a+b))
    elif op == "-":
        print("%d - %d = %d" %(a,b,a-b))
    elif op == "*" :
        print("%d * %d = %d" %(a,b,a*b))
    elif op == "/":
        print("%d / %d = %d" %(a,b,a/b))


################ ๋ฐ˜๋ณต๋ฌธ ์ œ์–ด (break) ################
# ๋ฐ˜๋ณต๋ฌธ์„ ํƒˆ์ถœํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค
# ctrl + c ์‹œ๊ทธ๋„์„ ์ฝ”๋”ฉ ์•ˆ์— ๋…น์—ฌ๋‚ธ๋‹ค

i=0
for i in range(1,100,1):
    print("for๋ฌธ์„ %d๋ฒˆ ์‹คํ–‰" %i)
    break



# ์ž…๋ ฅ์„ 0,q ๋ฐ›์•„์„œ if๋ฌธ์œผ๋กœ ๋ฐ›์•„์„œ ์‹คํ–‰
sum=0
a,b=0,0

while True :
    a=int(input("์ฒซ๋ฒˆ์งธ ์ˆ˜ ์ž…๋ ฅ : "))
    if a==0:
        break
    b = int(input("๋‘๋ฒˆ์งธ ์ˆ˜ ์ž…๋ ฅ : "))
    if b == 0:
        break

    sum = a+b
    print("%d + %d =%d " %(a,b,sum))

print("0์„ ์ž…๋ ฅํ•ด์„œ ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ")


# Quiz )
# 1~100๊นŒ์ง€ ํ•ฉ์„ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๊ตฌํ•˜๊ธฐ
# ํ•ฉ์ด 1000 ๋„˜์–ด๊ฐˆ ๋•Œ i ๊ฐ’์ด ๋ช‡์ธ์ง€ ํ™•์ธ

sum = 0
i=1

while True :
    i<101
    sum = sum + i
    i = i+1
    if sum>1000 :
        print("i์˜ ๊ฐ’์€ %d์ž…๋‹ˆ๋‹ค" %i)
        break



################ ๋ฐ˜๋ณต๋ฌธ ์ œ์–ด (continue) ################
# ์‹คํ–‰๋ฌธ ๋ธ”๋ก์˜ ๋‚จ์€ ๋ถ€๋ถ„์„ ๊ฑด๋„ˆ๋›ฐ๊ณ  ๋ฐ˜๋ณต๋ฌธ์˜ ์ฒ˜์Œ์œผ๋กœ ๋Œ์•„๊ฐ
# skip

# 3์˜ ๋ฐฐ์ˆ˜๋ฅผ ์ œ์™ธํ•œ 1~100๊นŒ์ง€ ํ•ฉ ๊ตฌํ•˜๊ธฐ
sum,i=0,0

for i in range(1,101):
    if i%3==0:
        continue
    sum = sum + i
    print(i)
print("1~100๊นŒ์ง€ 3์˜ ๋ฐฐ์ˆ˜ ์ œ์™ธ ํ•ฉ : %d" %sum)

# Quiz ) while ๋ฌธ์œผ๋กœ ์‚ฌ์šฉ
sum=0
i=1

while i<101:
    sum = sum + i
    i = i + 1

    if i%3==0:
        continue
    print(i)
print("1~100๊นŒ์ง€ 3์˜ ๋ฐฐ์ˆ˜ ์ œ์™ธ ํ•ฉ : %d" %sum)
๋ฐ˜์‘ํ˜•