¿ì¸®ÀÇ Ã¹¹ø° Á¦¾î ±¸Á¶¸¦ Á¦½ÃÇϷôÙ. Æò»óÀûÀ¸·Î ÄÄÇ»Åʹ ù ¹ø° ¶óÀο¡¼ ½ÃÀÛÇÏ¿© ¾Æ·¡·Î ³»·Á°£´Ù. Á¦¾î ±¸Á¶´Â ¾î¶² ¼¼ú¹®ÀÌ ½ÇÇàµÇ¸é ¼¼ú¹®ÀÌ ½ÇÇàµÇ°Å³ª °áÁ¤µÇ´Â ¼ø¼¸¦ ¹Ù²Û´Ù. ¿©±â¿¡ while Á¦¾î ±¸Á¶¸¦ »ç¿ëÇÏ´Â ÇÁ·Î±×·¥À» À§ÇÑ ¼Ò½º°¡ ÀÖ´Ù:
a = 0 while a < 10: a = a + 1 print a
±×¸®°í ¿©±â¿¡ Á¤¸»·Î Èï¹ÌÁøÁøÇÑ Ãâ·ÂÀÌ ÀÖ´Ù :
1 2 3 4 5 6 7 8 9 10
(¿©·¯ºÐÀÇ ÄÄÇ»Å͸¦ 5´Þ·¯Â¥¸® °è»ê±â·Î ¹Ù²Ù¾îµµ ´õ ³ª»Ü°Í °°Áö ¾Ê´Ù°í ¿©·¯ºÐÀº »ý°¢ÇÏ°ÚÁö?) ÀÚ ÀÌ ÇÁ·Î±×·¥Àº ¹«¾ùÀ» Çϴ°¡? ¸ÕÀú a = 0À̶ó´Â ¶óÀÎÀ» º¸°í¼ 0À» ¸¸µç´Ù. ±×¸®°í while a < 10:¶ó´Â ¶óÀÎÀ» º¸°í¼ ÄÄÇ»ÅÍ´Â a < 10Àΰ¡¸¦ Á¡°ËÇÑ´Ù. a´Â 0À̶ó´Â ¼¼ú¹®À» ÄÄÇ»ÅÍ°¡ óÀ½À¸·Î º¸¾ÒÀ¸¹Ç·Î ±×°ÍÀº 10 ÀÌÇÏÀÌ´Ù. ´Ù¸¥ ¸»·Î Çϸé a °¡ 10 ÀÌÇÏÀÎ µ¿¾È¿¡¸¸ ÄÄÇ»ÅÍ´Â µé¿©¾²±âµÈ ¼¼ú¹®µéÀ» ½ÇÇàÇÒ °ÍÀÌ´Ù.
¿©±â¿¡ whileÀ» »ç¿ëÇÏ´Â ¶Ç ´Ù¸¥ ¿¹Á¦°¡ ÀÖ´Ù:
a = 1 s = 0 print 'Enter Numbers to add to the sum.' print 'Enter 0 to quit.' while a != 0 : print 'Current Sum:',s a = input('Number? ') s = s + a print 'Total Sum =',s
óÀ½À¸·Î ³»°¡ ÀÌ ÇÁ·Î±×·¥À» ½ÇÇàÇßÀ¸¹Ç·Î ÆÄÀ̽ãÀº ´ÙÀ½°ú °°Àº °ÍÀ» ³» ¹ñ´Â´Ù:
File "sum.py", line 3 while a != 0 ^ SyntaxError: invalid syntax³ª´Â while¹®ÀÇ µÚ¿¡ :¸¦ ³õ´Â°ÍÀ» ±ôºýÇß´Ù. ¿¡·¯ ¸Þ½ÃÁö´Â ±× ¹®Á¦¿¡ ´ëÇØ ºÒÆòÀ» ÇÏ´Â °ÍÀ̸ç ÆÄÀ̽ãÀÌ »ý°¢Çϱ⿡ ¹®Á¦°¡ ¾îµðÀÎÁö¸¦
À²©¼è
^"·Î ÁöÀûÇÏ´Â °ÍÀÌ´Ù.
ÀÌ ¹®Á¦¸¦ ÇØ°áÇÑ ÈÄ¿¡ ³»°¡ ÇÁ·Î±×·¥À» ½ÇÇàÇÑ ¹Ù¸¦ ¿©±â¿¡ º¸À̸é :
Enter Numbers to add to the sum. Enter 0 to quit. Current Sum: 0 Number? 200 Current Sum: 200 Number? -15.25 Current Sum: 184.75 Number? -151.85 Current Sum: 32.9 Number? 10.00 Current Sum: 42.9 Number? 0 Total Sum = 42.9
Ç¥Çö½Ä "print 'Total Sum =',s "ÀÌ ¸¶Áö¸·¿¡ Çѹø ¸¸ ½ÇÇàµÊÀ» ÁÖ¸ñÇ϶ó. while ¼¼ú¹®Àº ÅǾ²±âµÈ ( µé¿©¾²±âµÈ) ¶óÀο¡¸¸ ¿µÇâÀ» ¹ÌÄ£´Ù. != Àº °°Áö ¾Ê´Ù´Â °ÍÀ» ÀǹÌÇÑ´Ù ±×·¡¼ while a != 0 : °¡ ¶æÇÏ´Â °ÍÀº a °¡ 0ÀÌ µÉ ¶§±îÁö ÀÌÈÄÀÇ ÅǾ²±âµÈ ¼¼ú¹®µéÀ» ½ÇÇàÇ϶ó´Â ÀǹÌÀÌ´Ù.
ÀÌÁ¦ ¿ì¸®´Â while ·çÇÁ¸¦ ´Ù·é´Ù, ¿µ¿øÈ÷ ½ÇÇàµÇ´Â ÇÁ·Î±×·¥À» °®´Â °ÍÀº °¡´ÉÇÏ´Ù. ÀÌ°ÍÀ» ÇÏ´Â ½¬¿î ¹æ¹ýÀº ÇÁ·Î±×·¥À» ´ÙÀ½°ú °°ÀÌ ÀÛ¼ºÇÏ´Â °ÍÀÌ´Ù:
while 1 == 1: print "Help, I'm stuck in a loop."
ÀÌ ÇÁ·Î±×·¥Àº ¿ìÁÖ°¡ ¿·Î ¸ê¸ÁÇϰųª ¿©·¯ºÐÀÌ ¸ØÃ⶧±îÁö "Help, I'm stuck in a loop."À» Ãâ·ÂÇÒ °ÍÀÌ´Ù. ¿©·¯ºÐÀÌ ¸ØÃß´Â ¹æ¹ýÀº Control (¶Ç´Â Ctrl) Å°¿Í `c' (¹®ÀÚ)¸¦ µ¿½Ã¿¡ ´©¸£´Â °ÍÀÌ´Ù. ÀÌ°ÍÀ¸·Î ±× ÇÁ·Î±×·¥Àº Á×À» °ÍÀÌ´Ù. (ÁÖÀÇ: ¶§·Î´Â ¿©·¯ºÐÀº Control C ¸¦ ´©¸¥ÈÄ¿¡ ¿£Å͸¦ Ãľ߸¸ ÇÒ °ÍÀÌ´Ù.)
Fibonnaci.py
#This program calulates the fibonnaci sequence a = 0 b = 1 count = 0 max_count = 20 while count < max_count: count = count + 1 #we need to keep track of a since we change it old_a = a a = b b = old_a + b #Notice that the , at the end of a print statement keeps it # from switching to a new line print old_a, print
Ãâ ·Â:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
Password.py
Waits until a password has been entered. Use control-C to break out with out # the password password = "foobar" #note that != means not equal while password != "unicorn": password = raw_input("Password:") print "Welcome in"
»ùÇà ½ÇÇà:
Password:auo Password:y22 Password:password Password:open seaseme Password:unicorn Welcome in