Coding/Python

[python] ์ œ์–ด๋ฌธ(if, for, while, continue, break)

๊น€์œ ๋‹ˆ์ฝ˜ 2021. 10. 20. 17:47

if

weather = input("์˜ค๋Š˜ ๋‚ ์”จ๋Š” ์–ด๋•Œ์š”?") #์‚ฌ์šฉ์ž ์ธํ’‹์„ ํ•„์š”๋กœ ํ•จ
if weather == "๋น„" or weather == "๋ˆˆ": 
	print("์šฐ์‚ฐ์„ ์ฑ™๊ธฐ์„ธ์š”") 
elif weather == "๋ฏธ์„ธ๋จผ์ง€":
	print("๋งˆ์Šคํฌ๋ฅผ ์ฑ™๊ธฐ์„ธ์š”.")
else:
	print("์ค€๋น„๋ฌผ ํ•„์š”์—†์–ด์š”.")

 

for

for waiting_no in range(1,6): #1, 2, 3, 4, 5
	print("๋Œ€๊ธฐ๋ฒˆํ˜ธ : {0}".format(waiting_no))
   

#์Šคํƒ€๋ฒ…์Šค ์†๋‹˜
starbucks = ["์•„์ด์–ธ๋งจ", "ํ† ๋ฅด", "์•„์ด์— ๊ทธ๋ฃจํŠธ"]
for cumstomer in starbucks: 
     print("{0}๋‹˜, ์ปคํ”ผ๊ฐ€ ์ค€๋น„๋˜์—ˆ์Šต๋‹ˆ๋‹ค.".format(customer))

while

#5๋ฒˆ์ด๋‚˜ ๋ถˆ๋ €๋Š”๋ฐ ์•ˆ๋‚˜ํƒ€๋‚˜๋ฉด ์ปคํ”ผ ๋ฒ„๋ฆฌ๋Š” ์ •์ฑ…
customer = "ํ† ๋ฅด"
index = 5
while index >= 1: 
     print("{0}๋‹˜, ์ปคํ”ผ๊ฐ€ ์ค€๋น„๋˜์—ˆ์Šต๋‹ˆ๋‹ค. {1}๋ฒˆ ๋‚จ์•˜์–ด์š”.".format(customer, index))
	index -=1
if index == 0 
     print("์ปคํ”ผ๋Š” ํ๊ธฐ์ฒ˜๋ถ„๋˜์—ˆ์Šต๋‹ˆ๋‹ค.") 

 

customer = "์•„์ด์–ธ๋งจ" 
index = 1
while True: 
       print("{0}๋‹˜, ์ปคํ”ผ๊ฐ€ ์ค€๋น„๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ˜ธ์ถœ{1}ํšŒ.".format(customer, index))
       index += 1    #๋ฌดํ•œ๋ฃจํ”„

 

 

customer = "ํ† ๋ฅด" 
person = "Unknown" 
while person != customer :
       print("{0}๋‹˜, ์ปคํ”ผ๊ฐ€ ์ค€๋น„๋˜์—ˆ์Šต๋‹ˆ๋‹ค.".format(customer))
       person = input("์ด๋ฆ„์ด ์–ด๋–ป๊ฒŒ ๋˜์„ธ์š”?")


continue and break

absent = [2, 5] #2๋ฒˆ๊ณผ 5๋ฒˆ ๊ฒฐ์„
no_book = [7] #์ฑ…์„ ์•ˆ๊ฐ–๊ณ ์˜ด 
for student in range(1, 11): #1~10๊นŒ์ง€ ์ถœ์„๋ฒˆํ˜ธ
     if student in absent: 
        continue
     elif student in no_book: 
        print("์˜ค๋Š˜ ์ˆ˜์—… ์—ฌ๊ธฐ๊นŒ์ง€. {0}๋Š” ๊ต๋ฌด์‹ค๋กœ ๋”ฐ๋ผ์™€.format(student))
		break
     print("{0}์•ผ, ์ฑ…์„ ์ฝ์–ด๋ด".format(stduent))

 

 

ํ•œ ์ค„ for

#์ถœ์„๋ฒˆํ˜ธ๊ฐ€ 1 2 3 4 ๊ฐ€ ์žˆ๋Š”๋ฐ, ์•ž์œผ๋กœ 100์„ ๋ถ™์ด๊ธฐ๋กœ ํ•จ
students = [1, 2, 3, 4, 5]
students [ i+100 for i in students]


#ํ•™์ƒ ์ด๋ฆ„์„ ๊ธธ์ด๋กœ ๋ณ€ํ™˜ 
students = ["Iron man", "Thor". "I am groot"]
students = [len(i) for i in students]


#ํ•™์ƒ ์ด๋ฆ„์„ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜
students = ["Iron man", "Thor". "I am groot"]
students = [i.upper() for i in students]

Quiz

ํƒ์‹œ ์„œ๋น„์Šค๋ฅผ ์ด์šฉํ•˜๋Š” ํƒ์‹œ ๊ธฐ์‚ฌ๋‹˜
50๋ช…์˜ ์Šน๊ฐ๊ณผ ๋งค์นญ ๊ธฐํšŒ๊ฐ€ ์žˆ์„ ๋–„, ์ด ํƒ‘์Šน ์Šน๊ฐ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์กฐ๊ฑด1: ์Šน๊ฐ๋ณ„ ์šดํ–‰ ์†Œ์š” ์‹œ๊ฐ„์€ 5~50๋ถ„ ์‚ฌ์ด์˜ ๋‚œ์ˆ˜๋กœ ์ •ํ•ด์ง‘๋‹ˆ๋‹ค.
์กฐ๊ฑด2: ๋‹น์‹ ์€ ์†Œ์š”์‹œ๊ฐ„ 5~15๋ถ„ ์‚ฌ์ด์˜ ์Šน๊ฐ๋งŒ ๋งค์นญํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ถœ๋ ฅ๋ฌผ ์˜ˆ์ œ:

[ ] 1๋ฒˆ์งธ ์†๋‹˜ (์†Œ์š”์‹œ๊ฐ„ : 20๋ถ„)
.
.
.
์ด ํƒ‘์Šน ์Šน๊ฐ : 2๋ถ„

๋‚˜์˜ ํ’€์ด 

๋”๋ณด๊ธฐ
from random import *
count = 0
for clients in range(1, 51):
    time = randint(5, 50)
    if 5 <= time <= 15:
        ox = "O"
        count += 1
        print("[{0}] {1}๋ฒˆ์งธ ์†๋‹˜ (์†Œ์š”์‹œ๊ฐ„: {2}๋ถ„)".format(ox,clients,time))

    else:
        ox = " "
        print("[{0}] {1}๋ฒˆ์งธ ์†๋‹˜ (์†Œ์š”์‹œ๊ฐ„: {2}๋ถ„)".format(ox, clients, time))

print("์ด ํƒ‘์Šน ์Šน๊ฐ : {}๋ถ„".format(count))

ํ•ด์„ค

๋”๋ณด๊ธฐ
from random import *
cnt = 0 #์ด ํƒ‘์Šน ์Šน๊ฐ ์ˆ˜
for i in range(1, 51):
    time = randrange(5, 51)
    if 5 <= time <= 15:
        print("[O] {0}๋ฒˆ์งธ ์†๋‹˜ (์†Œ์š”์‹œ๊ฐ„ : {1}๋ถ„))".format(i, time))
        cnt += 1

    else:
        print("[ ] {0}๋ฒˆ์งธ ์†๋‹˜ (์†Œ์š”์‹œ๊ฐ„ : {1}๋ถ„))".format(i, time))
print("์ด ํƒ‘์Šน ์Šน๊ฐ : {0}".format(cnt))