which_one = input("What month (1-12)? ") months = ['January', 'February', 'March', 'April', 'May', 'June', 'July',\ 'August', 'September', 'October', 'November', 'December'] if 1 <= which_one <= 12: print "The month is",months[which_one - 1]
±×¸®°í Ãâ·ÂÀÇ ¿¹´Â ´ÙÀ½°ú °°´Ù:
What month (1-12)? 3 The month is March
ÀÌ ¿¹¿¡¼ months°¡ ¸®½ºÆ®ÀÌ´Ù. months´Â ¶óÀÎ months = ['January', 'February', 'March', 'April', 'May', 'June', 'July',\
'August', 'September', 'October', 'November', 'December']·Î Á¤ÀÇ µÈ´Ù. (\
°¡ ±â´Ù¶õ ¶óÀÎÀ» ³ª´©´Âµ¥ »ç¿ëµÉ ¼ö ÀÖÀ½À» ÁÖ¸ñÇ϶ó.) [
¿Í ]
´Â °¢°¢ ½ÃÀÛ°ú ³¡À̸ç Äĸ¶(``,
'')·Î ¸®½ºÆ®ÀÇ Ç׸ñµéÀ» ºÐ¸®ÇÑ´Ù.
¸®½ºÆ®´Â 'months[which_one - 1]
'¿¡¼ »ç¿ëµÈ´Ù.
¸®½ºÆ®´Â 0¿¡¼ ½ÃÀÛÇÏ´Â Ç׸ñµé·Î ±¸¼ºµÈ´Ù. ´Ù¸¥ ¸»·Î Çϸé, ¿©·¯ºÐÀÌ 1¿ù´ÞÀ» ¿øÇÑ´Ù¸é ¿©·¯ºÐÀº months[0]¸¦ »ç¿ëÇÒ ¼ö ÀÖ´Ù. ¸®½ºÆ®¿¡ ¼ýÀÚ Çϳª¸¦ ÁÖ¾î¶ó ±×·¯¸é ±× À§Ä¡¿¡ ÀúÀåµÈ °ªÀ» µ¹·Á ÁÙ °ÍÀÌ´Ù.
¼¼ú¹® 'if 1 <= which_one <= 12:'´Â which_oneÀÌ 1°ú 12 »çÀÌ¿¡ Æ÷Ç﵃ ¶§¸¸ ¿ÀÁ÷ ÂüÀÌ µÉ °ÍÀÌ´Ù. (´Ù¸¥ ¸»·Î Çϸé, ¿©·¯ºÐÀÌ ´ë¼öÇп¡¼ ±×°ÍÀ» º¸¾Ò´Ù¸é ´ç¿¬È÷ ±×°ÍÀÌ ¿©·¯ºÐÀÌ ¿¹»óÇÑ ¹ÙÀÌ´Ù.).
¸®½ºÆ®¸¦ ÀÏ·ÃÀÇ »óÀÚ·Î »ý°¢ÇÒ ¼öµµ ÀÖ´Ù. ¿¹¸¦ µé¾î, 'demolist = ['life',42, 'the universe', 6,'and',7]'¿¡ ÀÇÇÏ¿© »ý¼ºµÇ¾îÁø »óÀÚµéÀº ´ÙÀ½°ú °°ÀÌ º¸ÀÏ °ÍÀÌ´Ù:
box number | 0 | 1 | 2 | 3 | 4 | 5 |
demolist | `life' | 42 | `the universe' | 6 | `and' | 7 |
°¢ »óÀÚµéÀº ±× ¼ýÀÚ¿¡ ÀÇÇØ ÂüÁ¶µÇ¾î¼ ¼¼ú¹® demolist[0]´Â 'life'¸¦, demolist[1]Àº 42¸¦ ȹµæÇÏ°Ô µÇ°í, ±×·±½ÄÀ¸·Î demolist[5]´Â 7¸¦ ȹµæÇÏ°Ô µÈ´Ù.
demolist = ['life',42, 'the universe', 6,'and',7] print 'demolist = ',demolist demolist.append('everything') print "after 'everything' was appended demolist is now:" print demolist print 'len(demolist) =', len(demolist) print 'demolist.index(42) =',demolist.index(42) print 'demolist[1] =', demolist[1] #Next we will loop through the list c = 0 while c < len(demolist): print 'demolist[',c,']=',demolist[c] c = c + 1 del demolist[2] print "After 'the universe' was removed demolist is now:" print demolist if 'life' in demolist: print "'life' was found in demolist" else: print "'life' was not found in demolist" if 'amoeba' in demolist: print "'amoeba' was found in demolist" if 'amoeba' not in demolist: print "'amoeba' was not found in demolist" demolist.sort() print 'The sorted demolist is ',demolist
±× Ãâ·ÂÀº ´ÙÀ½°ú °°´Ù:
demolist = ['life', 42, 'the universe', 6, 'and', 7] after 'everything' was appended demolist is now: ['life', 42, 'the universe', 6, 'and', 7, 'everything'] len(demolist) = 7 demolist.index(42) = 1 demolist[1] = 42 demolist[ 0 ]= life demolist[ 1 ]= 42 demolist[ 2 ]= the universe demolist[ 3 ]= 6 demolist[ 4 ]= and demolist[ 5 ]= 7 demolist[ 6 ]= everything After 'the universe' was removed demolist is now: ['life', 42, 6, 'and', 7, 'everything'] 'life' was found in demolist 'amoeba' was not found in demolist The sorted demolist is [6, 7, 42, 'and', 'everything', 'life']
ÀÌ ¿¹Á¦´Â »õ·Î¿î ÇÔ¼ö¸¦ ¹«´õ±â·Î »ç¿ëÇÏ°í ÀÖ´Ù. ¿©·¯ºÐÀº Àüü ¸®½ºÆ®¸¦ ´ÜÁö Ãâ·Â printÇÒ ¼ö¸¸ ÀÖÀ½À» ÁÖÀÇÇ϶ó. ´ÙÀ½À¸·Î append ÇÔ¼ö´Â ¸®½ºÆ®ÀÇ ³¡ÀÌ »õ·Î¿î Ç׸ñ Çϳª¸¦ ´õÇϴµ¥ »ç¿ëµÇ¾îÁø´Ù. len
ÇÔ¼ö´Â ¸®½ºÆ®¿¡ ¾ó¸¶³ª ¸¹Àº Ç׸ñÀÌ ÀÖ´ÂÁö¸¦ µ¹·ÁÁØ´Ù. The index ÇÔ¼ö´Â Ç׸ñÀÇ À§Ä¡°¡ ¸®½ºÆ®¿¡¼ ¾îµðÀÎÁö¸¦ ¸»ÇØÁØ´Ù.
'demolist.index(42)
' °¡ 1À» µ¹·ÁÁÖ°í demolist[1]
°¡ ½ÇÇàµÇ¸é 42¸¦ µ¹·ÁÁشٴ °ÍÀ» ÁÖ¸ñÇ϶ó.
'#Next we will loop through the list
'¶óÀÎÀº ÇÁ·Î±×·¡¸Ó¿¡°Ô´Â ´ÜÁö ¿©ºÐÀÌ´Ù( ¶ÇÇÑ ÁÖ¼®À̶ó°íµµ ºÎ¸¥´Ù).
ÆÄÀ̽ãÀº #
·Î ½ÃÀÛÇÏ´Â ¸ðµç ¶óÀÎÀ» ¹«½ÃÇÒ °ÍÀÌ´Ù.
´ÙÀ½ÀÇ ¶óÀÎÀº :
c = 0 while c < len(demolist): print 'demolist[',c,']=',demolist[c] c = c + 10¿¡¼ ½ÃÀÛÇؼ ¸®½ºÆ®ÀÇ ¸¶Áö¸· ÁöÇ¥¿¡ µµ´ÞÇÒ ¶§±îÁö Áõ°¡ÇÏ´Â ÇϳªÀÇ º¯¼ö c ¸¦ ¸¸µç´Ù. ±×µ¿¾È print¼¼ú¹®Àº ±× ¸®½ºÆ®ÀÇ °¢ ¿ä¼Ò¸¦ Ãâ·ÂÇÑ´Ù.
del
¸í·É¾î´Â ¸®½ºÆ®¿¡¼ ÁÖ¾îÁø ¿ä¼Ò¸¦ Á¦°ÅÇϴµ¥¿¡ »ç¿ëµÈ´Ù. ´ÙÀ½ÀÇ ¶óÀÎ ¸îÁÙÀº in ¿¬»êÀÚ¸¦ »ç¿ëÇÏ¿© ÇϳªÀÇ ¿ä¼Ò°¡ ¸®½ºÆ®¿¡ Æ÷ÇԵǾú´ÂÁö ¾Æ´ÑÁö¸¦ Å×½ºÆ®ÇÑ´Ù.
ÀÌ ¿¹Á¦´Â ÀÌ·¯ÇÑ »ç¾çÀ» ´õ À¯¿ëÇÏ°Ô »ç¿ëÇÑ´Ù:
menu_item = 0 list = [] while menu_item != 9: print "--------------------" print "1. Print the list" print "2. Add a name to the list" print "3. Remove a name from the list" print "4. Change an item in the list" print "9. Quit" menu_item = input("Pick an item from the menu: ") if menu_item == 1: current = 0 if len(list) > 0: while current < len(list): print current,". ",list[current] current = current + 1 else: print "List is empty" elif menu_item == 2: name = raw_input("Type in a name to add: ") list.append(name) elif menu_item == 3: del_name = raw_input("What name would you like to remove: ") if del_name in list: item_number = list.index(del_name) del list[item_number] else: print del_name," was not found" elif menu_item == 4: old_name = raw_input("What name would you like to change: ") if old_name in list: item_number = list.index(old_name) new_name = raw_input("What is the new name: ") list[item_number] = new_name else: print old_name," was not found" print "Goodbye"
±×¸®°í ¿©±â¿¡ Ãâ·ÂÀÇ ÀϺΰ¡ ÀÖ´Ù:
-------------------- 1. Print the list 2. Add a name to the list 3. Remove a name from the list 4. Change an item in the list 9. Quit Pick an item from the menu: 2 Type in a name to add: Jack Pick an item from the menu: 2 Type in a name to add: Jill Pick an item from the menu: 1 0 . Jack 1 . Jill Pick an item from the menu: 3 What name would you like to remove: Jack Pick an item from the menu: 4 What name would you like to change: Jill What is the new name: Jill Peters Pick an item from the menu: 1 0 . Jill Peters Pick an item from the menu: 9 Goodbye
±ä ÇÁ·Î±×·¥ÀÌ´Ù. ¼Ò½ºÄڵ带 Çѹø »ìÆ캸ÀÚ. ¶óÀÎ 'list = []'´Â º¯¼ölist¸¦ ¾Æ¹«·± Ç׸ñ(ȤÀº ¿ä¼Ò)µµ °°Áö ¾Ê´Â ¸®½ºÆ®·Î ¸¸µç´Ù. ´ÙÀ½ÀÇ Áß¿äÇÑ ¶óÀÎÀº while menu_item != 9:
ÀÌ´Ù. ÀÌ ¶óÀÎÀº ÀÌ ÇÁ·Î±×·¥À» À§ÇÑ ¸Þ´º½Ã½ºÅÛÀ» °¡´ÉÇÏ°Ô ÇØÁִ ȸµ¹À̸¦ ½ÃÀÛÇÑ´Ù. ´ÙÀ½ÀÇ ¶óÀÎ ¸î°³´Â ¸Þ´º¸¦ º¸¿©ÁÖ°í, ÇÁ·Î±×·¥ÀÇ ¾î¶² ºÎºÐÀÌ ½ÇÇàµÉÁö¸¦ °áÁ¤ÇÑ´Ù.
´ÙÀ½ÀÇ ÄÚµå´Â :
current = 0 if len(list) > 0: while current < len(list): print current,". ",list[current] current = current + 1 else: print "List is empty"¸®½ºÆ®¸¦ ¼øȸÇÏ¸é¼ °¢°¢ÀÇ À̸§À» Ãâ·ÂÇÑ´Ù.
len(list_name)
´Â ¸®½ºÆ®¿¡ ¾ó¸¶³ª ¸¹Àº Ç׸ñÀÌ ÀÖ´ÂÁö ¾Ë·ÁÁØ´Ù. len ÇÔ¼ö°¡ 0
À» ¹ÝȯÇÏ¸é ¸®½ºÆ®´Â ºñ¾î ÀÖ´Â °ÍÀÌ´Ù.
±×¸®°í ³ª¼ ¸î°³ÀÇ ¶óÀÎ µÚ¿¡ ¼¼ú¹® 'list.append(name)'°¡ ³ªÅ¸³´Ù. ±×°ÍÀº appendÇÔ¼ö¸¦ »ç¿ëÇÏ¿© ¸®½ºÆ®ÀÇ ³¡¿¡´Ù°¡ ÇÑ°³ÀÇ Ç׸ñÀ» Ãß°¡ÇÑ´Ù. ¾Æ·¡ÀÇ µÎ°³ ¶óÀÎÀ¸·Î Á¡ÇÁÇ϶ó. ÀÌ ÄÚµåÀÇ ºÎºÐÀº ´ÙÀ½°ú °°´Ù:
item_number = list.index(del_name) del list[item_number]¿©±â¿¡¼ indexÇÔ¼ö°¡ »ç¿ëµÇ¾î ±× Ç׸ñÀ» Á¦°ÅÇϱâ À§ÇÏ¿© ³ªÁß¿¡ »ç¿ëÇÏ°Ô µÉ ÁöÇ¥°ªÀ» Ž»öÇÑ´Ù. '
del list[item_number]
'´Â ¸®½ºÆ®¿¡¼ ÇÑ°³ÀÇ Ç׸ñÀ» Á¦°ÅÇϱâ À§ÇÏ¿© »ç¿ëµÈ´Ù.
´ÙÀ½ ºÎºÐÀº :
old_name = raw_input("What name would you like to change: ") if old_name in list: item_number = list.index(old_name) new_name = raw_input("What is the new name: ") list[item_number] = new_name else: print old_name," was not found"'index'¸¦ »ç¿ëÇÏ¿©
item_number
¸¦ Ž»öÇؼ old_name
ÀÌ ÀÖ¾ú´ø °÷¿¡ new_name
¸¦ ³õ´Â´Ù.
test.py
## This program runs a test of knowledge true = 1 false = 0 # First get the test questions # Later this will be modified to use file io. def get_questions(): # notice how the data is stored as a list of lists return [["What color is the daytime sky on a clear day?","blue"],\ ["What is the answer to life, the universe and everything?","42"],\ ["What is a three letter word for mouse trap?","cat"]]
# This will test a single question # it takes a single question in # it returns true if the user typed the correct answer, otherwise false def check_question(question_and_answer): #extract the question and the answer from the list question = question_and_answer[0] answer = question_and_answer[1] # give the question to the user given_answer = raw_input(question) # compare the user's answer to the testers answer if answer == given_answer: print "Correct" return true else: print "Incorrect, correct was:",answer return false
# This will run through all the questions def run_test(questions): if len(questions) == 0: print "No questions were given." # the return exits the function return index = 0 right = 0 while index < len(questions): #Check the question if check_question(questions[index]): right = right + 1 #go to the next question index = index + 1 #notice the order of the computation, first multiply, then divide print "You got ",right*100/len(questions),"% right out of",len(questions) #now lets run the questions run_test(get_questions())
»ùÇà Ãâ·ÂÀº ´ÙÀ½°ú °°´Ù:
What color is the daytime sky on a clear day?green Incorrect, correct was: blue What is the answer to life, the universe and everything?42 Correct What is a three letter word for mouse trap?cat Correct You got 66 % right out of 3