print("ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!")
print("ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!")
print("ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!")
print("ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!")
print("ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!")
for i in range(100):
print("ÆÄÀ̽ã ÁÖ½Äȸ»çÀÇ ¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!")
5.3 Ƚ¼ö¸¦ Á¤Çسõ°í ¹Ýº¹½ÃÅ°ÀÚ
for i in [1, 2, 3, 4, 5]: # ³¡¿¡ :ÀÌ ÀÖ¾î¾ß ÇÔ
print("¹æ¹®À» ȯ¿µÇÕ´Ï´Ù.") # µé¿©¾²±âÇÏ¿©¾ß ÇÔ
5.4 for - in ´ÙÀ½¿¡´Â ¸®½ºÆ®³ª ¹®ÀÚ¿µµ ¿Ã ¼ö ÀÖ´Ù
for i in [1, 2, 3, 4, 5]:
print("i=", i)
for i in "Hello": # ³¡¿¡ :ÀÌ ÀÖ¾î¾ß ÇÔ
print("i =", i) # i °ªÀ» Ãâ·ÂÇغ¸ÀÚ
for i in [1, 2, 3, 4, 5]:
print("9*", i, "=", 9*i)
5.5 for ¹®°úÀÇ Âû¶± ±ÃÇÕÀÎ range() ÇÔ¼ö
for i in range(5): # (1)
print("¹æ¹®À» ȯ¿µÇÕ´Ï´Ù!") # (2)
list(range(10))
5.6 range() ÇÔ¼ö´Â ¼ýÀÚ¸¦ »ý»êÇÏ´Â °øÀåÀÌ´Ù
for i in range(1, 6, 1):
print(i, end=" ")
for i in range(10, 0, -1):
print(i, end=" ")
LAB5-1 ÅÍƲ ±×·¡ÇÈÀ¸·Î ¿©·¯°³ÀÇ ¿øÀ» ±×·Á º¸ÀÚ
import turtle
t = turtle.Turtle()
t.circle(100) # ¹ÝÁö¸§ÀÌ 100ÀÎ ¿øÀ» ±×¸°´Ù.
t.left(60) # 60µµ ¸¸Å ÅÍƲÀ» ¿ÞÂÊÀ¸·Î ȸÀü½ÃŲ´Ù.
t.circle(100)
t.left(60)
t.circle(100)
t.left(60)
t.circle(100)
t.left(60)
t.circle(100)
t.left(60)
t.circle(100)
turtle.done()
try:
turtle.bye()
except:
print("bye")
import turtle
t = turtle.Turtle()
for count in range(6):
t.circle(100)
t.left(360/6)
turtle.done()
try:
turtle.bye()
except:
print("bye")
LAB5-2 ¹Ýº¹À» »ç¿ëÇÏ¿© µµÇüÀ» ±×¸®ÀÚ
import turtle
t = turtle.Turtle()
t.shape("turtle")
# Á¤»ï°¢Çü ±×¸®±â
for i in range(3):
t.forward(100)
t.left(360/3) # 360/3À» ÅëÇØ 120µµ ¿ÞÂÊÀ¸·Î Ʋ±â
# Á¤»ç°¢ÇüÀ» ±×¸®±â À§ÇÏ¿© ÅÍƲÀ» À̵¿Çϱâ
t.penup() # Ææ ÀÚ±¹ÀÌ ³²Áö ¾Êµµ·Ï ÆæÀ» µé¾î¼ À̵¿ÇÑ´Ù
t.goto(200, 0)
t.pendown() # À̵¿À» ¸¶Ä¡¸é ÆæÀº ³»¸®µµ·Ï ÇÑ´Ù
# Á¤»ç°¢Çü ±×¸®±â
for i in range(4):
t.forward(100)
t.left(360/4) # 360/4¸¦ ÅëÇØ 90µµ ¿ÞÂÊÀ¸·Î Ʋ±â
turtle.done()
try:
turtle.bye()
except:
print("bye")
LAB5-3 N-°¢Çü ±×¸®±â
import turtle
t = turtle.Turtle()
t.shape("turtle")
s = turtle.textinput("", "¸î°¢ÇüÀ» ¿øÇϽóª¿ä?:")
n=int(s)
for i in range(n):
t.forward(100)
t.left(360/n)
turtle.done()
try:
turtle.bye()
except:
print("bye")
LAB5-4 ¼ú¿¡ ÃëÇÑ °ÅºÏÀ̸¦ ±×¸®´Â ·£´ýÇÔ¼ö
import random
number = random.randint(1, 100)
import turtle
import random
t = turtle.Turtle()
t.shape("turtle")
for i in range(30):
length = random.randint(1, 100)
t.forward(length)
angle = random.randint(-180, 180)
t.right(angle)
turtle.done()
try:
turtle.bye()
except:
print("bye")
LAB5-5 ¹Ýº¹À» ÀÌ¿ëÇÏ¿© ÆÑÅ丮¾óÀ» °è»êÇϱâ
n = int(input("Á¤¼ö¸¦ ÀÔ·ÂÇϽÿÀ: "))
fact = 1
for i in range(1, n+1):
fact = fact * i
print(n, "!Àº", fact, "ÀÌ´Ù.")
5.7 Á¶°Ç¿¡ µû¶ó ¹Ýº¹Çؼ ½ÇÇàÇÏ´Â while ¹®
under_construction = True
while under_construction:
response = input("°ø»ç°¡ ¿Ï·áµÇ¾ú½À´Ï±î?");
if response == "¿¹" :
under_construction = False
print("·çÇÁ¸¦ Å»ÃâÇß½À´Ï´Ù.")
LAB5-6 »ç¿ëÀڷκÎÅÍ ¾ÏÈ£¸¦ ¹Þ¾Æ ·Î±×ÀÎÇϱâ
password = ""
while password != "pythonisfun":
password = input("¾ÏÈ£¸¦ ÀÔ·ÂÇϽÿÀ: ")
print("** ·Î±×ÀÎ ¼º°ø **")
5.8 ÀÏÁ¤ÇÑ È½¼ö ¹Ýº¹¿¡ while »ç¿ëÇϱâ
count = 1
sum = 0
while count <= 10 :
sum = sum + count
count = count + 1
print("ÇÕ°è´Â", sum)
import turtle
t = turtle.Turtle()
i = 0
while i < 4:
t.forward(100)
t.right(90)
i = i + 1
turtle.done()
try:
turtle.bye()
except:
print("bye")
LAB5-7 ÀԷ¹ÞÀº ¼ö¸¦ »ç¿ëÇÏ´Â ±¸±¸´Ü Ãâ·Â
dan = int(input("¿øÇÏ´Â ´ÜÀº: "))
i = 1
while i <= 9:
print("%s*%s=%s" % (dan, i, dan*i))
i = i + 1
LAB5-8 while ¹®À¸·Î º° ±×¸®±â¸¦ Çغ¸ÀÚ
import turtle
t = turtle.Turtle()
t.shape("turtle")
i = 0
while i < 5:
t.forward(50)
t.right(144)
i = i + 1
turtle.done()
try:
turtle.bye()
except:
print("bye")
LAB5-9 °£´ÜÇÑ ÄÚµå·Î ¸ÚÁø ³ª¼±Çü µµÇü ±×·Áº¸±â
import turtle
t = turtle.Turtle()
# °ÅºÏÀÌÀÇ ¼Óµµ´Â 0À¸·Î ¼³Á¤Çϸé ÃÖ´ë°¡ µÈ´Ù.
t.speed(0)
t.width(3)
length = 10 # Ãʱ⠼±ÀÇ ±æÀÌ´Â 10À¸·Î ÇÑ´Ù.
# while ¹Ýº¹¹®ÀÌ´Ù. ¼±ÀÇ ±æÀÌ°¡ 500º¸´Ù ÀÛÀ¸¸é ¹Ýº¹ÇÑ´Ù.
while length < 500:
t.forward(length) # length¸¸Å ÀüÁøÇÑ´Ù.
t.right(89) # 89µµ ¿À¸¥ÂÊÀ¸·Î ȸÀüÇÑ´Ù.
length += 5 # ¼±ÀÇ ±æÀ̸¦ 5¸¸Å Áõ°¡½ÃŲ´Ù.
turtle.done()
try:
turtle.bye()
except:
print("bye")
LAB5-9 °£´ÜÇÑ ÄÚµå·Î ¸ÚÁø ³ª¼±Çü µµÇü ±×·Áº¸±â
total = 0
answer = 'yes'
while answer == 'yes':
number = int(input('¼ýÀÚ¸¦ ÀÔ·ÂÇϽÿÀ: '))
total = total + number
answer = input('°è¼Ó?(yes/no): ')
print('ÇÕ°è´Â : ', total)
LAB5-11 ¹«ÇÑ ¹Ýº¹¹®À¸·Î ¼ýÀÚ ¸ÂÃ߱⠰ÔÀÓÀ» ¸¸µéÀÚ
import random
tries = 0
guess = 0;
answer = random.randint(1, 100)
print("1ºÎÅÍ 100 »çÀÌÀÇ ¼ýÀÚ¸¦ ¸ÂÃ߽ÿÀ")
while guess != answer:
guess = int(input("¼ýÀÚ¸¦ ÀÔ·ÂÇϽÿÀ: "))
tries = tries + 1
if guess < answer:
print("³·À½!")
elif guess > answer:
print("³ôÀ½!")
print("ÃàÇÏÇÕ´Ï´Ù. ÃÑ ½ÃµµÈ½¼ö=", tries)
LAB5-12 ¾Ï»ê ¹®Á¦¸¦ ¸¸µé¾îº¸ÀÚ
import random
i=0
while True:
x = random.randint(1, 100)
y = random.randint(1, 100)
print(x, '+', y, '=', end = ' ')
answer = int(input())
if answer == x + y:
print('ÀßÇß¾î¿ä!!')
else:
print('Á¤´äÀº',x+y,'ÀÔ´Ï´Ù. ´ÙÀ½¹ø¿¡´Â ÀßÇÒ ¼ö ÀÖÁÒ?')
i+=1
if(i==4):
break;
LAB5-13 â¾÷ÀÚ¸¦ À§ÇÑ ±â´É : ¸ðµç »÷µåÀ§Ä¡ Á¾·ù Ãâ·ÂÇϱâ
breads = ["È£¹Ð»§", "À§Æ®", "ÈÀÌÆ®" ]
meats = ["¹ÌÆ®º¼", "¼Ò½ÃÁö", "´ß°¡½¿»ì"]
vegis = ["¾ç»óÃß", "Å丶Åä", "¿ÀÀÌ"]
sauces = ["¸¶¿ä³×Áî", "Çã´Ï ¸Ó½ºÅ¸µå", "Ä¥¸®"]
print('´Þ¼ö³× »÷µåÀ§Ä¡ °¡°ÔÀÇ °¡´ÉÇÑ Á¶ÇÕ')
for b in breads:
for m in meats:
for v in vegis:
for s in sauces:
print(b+" + "+m+" + "+v+" + "+s)
5.9 ¹«ÇÑ ·çÇÁ¿Í break·Î ºüÁ®³ª°¡±â
while True:
light = input('½ÅÈ£µî »ö»óÀ» ÀÔ·ÂÇϽÿÀ:')
if light == 'green':
break
print('ÀüÁø!!')
5.10 ·çÇÁ¸¦ Á¦¾îÇÏ´Â °í±Þ ±â¹ý : continue¿Í break
st = 'I love Python Programming' # Ãâ·ÂÀ» À§ÇÑ ¹®ÀÚ¿
for ch in st:
if ch in ['a','e','i','o','u', 'A','E','I','O','U']:
continue # ¸ðÀ½ÀÏ °æ¿ì ¾Æ·¡ Ãâ·ÂÀ» °Ç³Ê¶Ú´Ù
print(ch, end='')
5.11 Ãâ·ÂÀ» ¿¹»Ú°Ô ¸¸µå´Â Æ÷¸ÅÆÃ
'{} Python!'.format('Hello')
'I like {} and {}'.format('Python', 'Java')
'I like {0} and {1}'.format('Python', 'Java')
'I like {1} and {0}'.format('Python', 'Java')
'I like {0}, {0}, {0}'.format('Python', 'Java')
'¼Ò¼öÁ¡ ¾Æ·¡ µÎÀÚ¸® Á¤¹Ðµµ : {0:.2f}, ¼¼ÀÚ¸® Á¤¹Ðµµ {0:.3f}'.format(1/3)
'Àüü 10ÄÀ» Â÷ÁöÇÏ´Â ½Ç¼ö : {0:10.3f}, {0:10.4f}'.format(3.1415926)
for i in range(2, 11, 2):
print('{0:3d} {1:4d} {2:5d}'.format(i, i*i, i*i*i))