next up previous contents
Next: ¸ðµâ »ç¿ëÇϱâ Up: ÀϹÝÀÎÀ» À§ÇÑ ÆÄÀ̽ã Áöħ¼­ Previous: ºÎ¿ï Ç¥Çö½Ä   ¸ñ Â÷

ȍ ˟

ÀÌ ÀåÀº »çÀü¿¡ °üÇÑ °ÍÀÌ´Ù. »çÀüÀº Å°¿Í °ªÀ» °¡Áø´Ù. Å°´Â °ªÀ» ã´Âµ¥ »ç¿ëµÈ´Ù. ¿©±â¿¡ »çÀüÀÌ »ç¿ëµÇ´Â ¿¹Á¦ Çϳª°¡ ÀÖ´Ù:
def print_menu():
    print '1. Print Phone Numbers'
    print '2. Add a Phone Number'
    print '3. Remove a Phone Number'
    print '4. Lookup a Phone Number'
    print '5. Quit'
    print
numbers = {}
menu_choice = 0
print_menu()
while menu_choice != 5:
    menu_choice = input("Type in a number (1-5):")
    if menu_choice == 1:
        print "Telephone Numbers:"
        for x in numbers.keys():
            print "Name: ",x," \tNumber: ",numbers[x]
        print
    elif menu_choice == 2:
        print "Add Name and Number"
        name = raw_input("Name:")
        phone = raw_input("Number:")
        numbers[name] = phone
    elif menu_choice == 3:
        print "Remove Name and Number"
        name = raw_input("Name:")
        if numbers.has_key(name):
            del numbers[name]
        else:
            print name," was not found"
    elif menu_choice == 4:
        print "Lookup Number"
        name = raw_input("Name:")
        if numbers.has_key(name):
            print "The number is",numbers[name]
        else:
            print name," was not found"
    elif menu_choice != 5:
        print_menu()
±×¸®°í ¿©±â¿¡ ³ªÀÇ Ãâ·ÂÀ» º¸Àδٸé:
1. Print Phone Numbers
2. Add a Phone Number
3. Remove a Phone Number
4. Lookup a Phone Number
5. Quit

Type in a number (1-5):2
Add Name and Number
Name:Joe
Number:545-4464
Type in a number (1-5):2
Add Name and Number
Name:Jill
Number:979-46^H54
Type in a number (1-5):2
Add Name and Number
Name:Fred
Number:132-9874
Type in a number (1-5):1
Telephone Numbers:
Name:  Jill     Number:  979-454
Name:  Joe      Number:  545-4464
Name:  Fred     Number:  132-9874

Type in a number (1-5):4
Lookup Number
Name:Joe
The number is 545-4464
Type in a number (1-5):3
Remove Name and Number
Name:Fred
Type in a number (1-5):1
Telephone Numbers:
Name:  Jill     Number:  979-454
Name:  Joe      Number:  545-4464

Type in a number (1-5):5
ÀÌ ÇÁ·Î±×·¥Àº ¸®½ºÆ®¿¡ °üÇÑ Àü Àå¿¡¼­ÀÇ 'name' ¸®½ºÆ®¿Í ºñ½ÁÇÏ´Ù. ¿©±â¿¡ ±× ÇÁ·Î±×·¥ÀÌ ¾î¶»°Ô ÀÛµ¿ÇÏ´Â Áö¸¦ º¸ÀδÙ. ¸ÕÀú print_menu°¡ Á¤ÀÇ µÈ´Ù. print_menu ´Â ´ÜÁö ¸Þ´º¸¦ Ãâ·ÂÇÒ »ÓÀ¸·Î ÇÁ·Î±×·¥¿¡¼­ ³ªÁß¿¡ µÎ ¹ø »ç¿ëµÈ´Ù. ´ÙÀ½¿¡´Â ¿ì½º²Î½º·´°Ô º¸ÀÌ´Â 'numbers = {}' ¶óÀÎÀÌ ¿Â´Ù. ÀÌ ¶óÀÎÀÌ ÇÏ´Â ¸ðµç ÀÏÀº ÆÄÀ̽㿡°Ô numbers °¡ »çÀüÀ̶ó°í ¾Ë·Á ÁÖ´Â °ÍÀÌ´Ù. ´ÙÀ½ÀÇ ¶óÀεéÀº ´ÜÁö ¸Þ´º°¡ ÀÛµ¿Çϵµ·Ï ÇÏ´Â °ÍÀÌ´Ù. ´ÙÀ½ÀÇ ¶óÀÎÀº:
for x in numbers.keys():
    print "Name: ",x," \tNumber: ",numbers[x]
»çÀüÀ» ¼øȸÇϸ鼭 ¸ðµç Á¤º¸µéÀ» Ãâ·ÂÇÑ´Ù. numbers.keys() ÇÔ¼ö´Â ÀÌÁ¦ for ȸµ¹ÀÌ¿¡ ÀÇÇØ »ç¿ëµÇ´Â ¸®½ºÆ® Çϳª¸¦ ¹ÝȯÇÑ´Ù. keys °¡ ¹ÝȯÇÑ ¸®½ºÆ®´Â ¾î¶² Ưº°ÇÑ ¼ø¼­°¡ ¾øÀ¸¹Ç·Î ¿©·¯ºÐÀÌ ¾ËÆĺª¼øÀ» ¿øÇÑ´Ù¸é ±×°ÍÀ» Á¤·ÄÇؾ߸¸ ÇÑ´Ù. ¸®½ºÆ®¿Í ºñ½ÁÇÏ°Ô numbers[x] ´Â »çÀü¿¡ Àִ Ưº°ÇÑ ±¸¼º¿ø¿¡ Á¢±ÙÇϴµ¥ »ç¿ëµÈ´Ù. ¹°·Ð ÀÌ °æ¿ì¿¡ x ´Â ¹®ÀÚ¿­ÀÌ´Ù. ´ÙÀ½À¸·Î 'numbers[name] = phone' ¶óÀÎÀº À̸§°ú ÀüÈ­¹øÈ£¸¦ ±× »çÀü¿¡ Ãß°¡ÇÑ´Ù. ¸¸¾à name ÀÌ ÀÌ¹Ì »çÀü¿¡ ÀÖ´Ù¸é phone Àº ±×Àü¿¡ ÀÖ¾ú´ø °ªÀ» ´ëüÇÒ °ÍÀÌ´Ù. ´ÙÀ½ÀÇ ¶óÀÎÀº :
if numbers.has_key(name):
    del numbers[name]
»çÀü¿¡ nameÀÌ ÀÖ´ÂÁö »ìÆ캻´Ù. ±×¸®°í ±×°ÍÀÌ Á¸ÀçÇÑ´Ù¸é Á¦°ÅÇÑ´Ù. numbers.has_key(name) ÇÔ¼ö´Â nameÀÌ numbers ¼Ó¿¡ ÀÖ´Ù¸é Âü°ªÀ» ±×·¸Áö ¾ÊÀ¸¸é °ÅÁþÀ» ¹ÝȯÇÑ´Ù. 'del numbers[name]'¶óÀÎÀº nameÅ°¿Í ±×¿Í °ü·ÃµÈ °ªÀ» Á¦°ÅÇÑ´Ù. ´ÙÀ½ÀÇ ¶óÀÎÀº:
if numbers.has_key(name):
    print "The number is",numbers[name]
»çÀüÀÌ ¾î¶²Å°¸¦ °¡Áö°í ÀÖ´ÂÁö Á¡°ËÇÏ°í ¸¸¾à ±×·¸´Ù¸é ±×°Í°ú °ü·ÃµÈ ¼ýÀÚ¸¦ Ãâ·ÂÇÑ´Ù. ¸¶Áö¸·À¸·Î ¸Þ´º¼±ÅÃÀÌ ¹«È¿ÇÏ´Ù¸é ¿©·¯ºÐÀÇ ´«À» ÆíÇÏ°Ô Çϱâ À§ÇØ ¸Þ´º¸¦ ´Ù½Ã Ãâ·ÂÇÑ´Ù.

¿ä ¾à: »çÀüÀº Å°¿Í °ªµéÀ» °¡Áø´Ù. Å°´Â ¹®ÀÚ¿­ ȤÀº ¼ýÀÚÀÏ ¼ö ÀÖ´Ù. Å°´Â °ªÀ» Áö½ÃÇÑ´Ù. °ªÀº ¾î¶°ÇÑ ÇüÅÂÀÇ º¯¼ö¶óµµ °¡´ÉÇÏ´Ù.(¸®½ºÆ® ȤÀº »çÀü±îÁöµµ Æ÷ÇÔÇÏ¿© (±×·¯ÇÑ »çÀüÀ̳ª ¸®½ºÆ®´Â ¹°·Ð »çÀüÀ̳ª ¸®½ºÆ® ÀڽŵéÀ» Æ÷ÇÔÇÒ ¼ö ÀÖ´Ù.( °ÅÀÇ ¸Â°ÎÁö¿ä?:)) ¿©±â¿¡ »çÀü¿¡¼­ ¸®½ºÆ®¸¦ »ç¿ëÇÏ´Â ¿¹Á¦ Çϳª°¡ ÀÖ´Ù:

max_points = [25,25,50,25,100]
assignments = ['hw ch 1','hw ch 2','quiz   ','hw ch 3','test']
students = {'#Max':max_points}

def print_menu():
    print "1. Add student"
    print "2. Remove student"
    print "3. Print grades"
    print "4. Record grade"
    print "5. Print Menu"
    print "6. Exit"
def print_all_grades():
        print '\t',
        for i in range(len(assignments)):
            print assignments[i],'\t',
        print
        keys = students.keys()
        keys.sort()
        for x in keys:
            print x,'\t',
            grades = students[x]
            print_grades(grades)
def print_grades(grades):
    for i in range(len(grades)):
        print grades[i],'\t\t',
    print

print_menu()
menu_choice = 0
while menu_choice != 6:
    print
    menu_choice = input("Menu Choice (1-6):")
    if menu_choice == 1:
        name = raw_input("Student to add:")
        students[name] = [0]*len(max_points)
    elif menu_choice == 2:
        name = raw_input("Student to remove:")
        if students.has_key(name):
            del students[name]
        else:
            print "Student: ",name," not found"
    elif menu_choice == 3:
        print_all_grades()

    elif menu_choice == 4:
        print "Record Grade"
        name = raw_input("Student:")
        if students.has_key(name):
            grades = students[name]
            print "Type in the number of the grade to record"
            print "Type a 0 (zero) to exit"
            for i in range(len(assignments)):
                print i+1,' ',assignments[i],'\t',
            print
            print_grades(grades)
            which = 1234
            while which != -1:
                which = input("Change which Grade:")
                which = which-1
                if 0 <= which < len(grades):
                    grade = input("Grade:")
                    grades[which] = grade
                elif which != -1:
                    print "Invalid Grade Number"
        else:
            print "Student not found"
    elif menu_choice != 6:
        print_menu()
±×¸®°í ¿©±â¿¡ »ùÇà Ãâ·ÂÀÌ ÀÖ´Ù:
1. Add student
2. Remove student
3. Print grades
4. Record grade
5. Print Menu
6. Exit

Menu Choice (1-6):3
        hw ch 1         hw ch 2         quiz            hw ch 3         test
#Max    25              25              50              25              100
Menu Choice (1-6):6
1. Add student
2. Remove student
3. Print grades
4. Record grade
5. Print Menu
6. Exit

Menu Choice (1-6):1
Student to add:Bill
Menu Choice (1-6):4
Record Grade
Student:Bill
Type in the number of the grade to record
Type a 0 (zero) to exit
1   hw ch 1     2   hw ch 2     3   quiz        4   hw ch 3     5   test
0               0               0               0               0
Change which Grade:1
Grade:25
Change which Grade:2
Grade:24
Change which Grade:3
Grade:45
Change which Grade:4
Grade:23
Change which Grade:5
Grade:95
Change which Grade:0
Menu Choice (1-6):3
        hw ch 1         hw ch 2         quiz            hw ch 3         test
#Max    25              25              50              25              100
Bill    25              24              45              23              95

Menu Choice (1-6):6
¿©±â¿¡ ±× ÇÁ·Î±×·¥ÀÌ µ¿ÀÛÇÏ´Â ¹ýÀ» º¸ÀδÙ. ±âº»ÀûÀ¸·Î º¯¼ö students ´Â ÇлýÀÇ À̸§À» Å°·Î ±×µéÀÇ ÇгâÀ» °ªÀ¸·Î °¡Áö´Â »çÀüÀÌ´Ù. ù ¹ø°ÀÇ µÎ ¶óÀÎÀº ´ÜÁö µÎ °³ÀÇ ¸®½ºÆ®¸¦ »ý¼ºÇÑ´Ù. ´ÙÀ½ÀÇ ¶óÀÎ 'students = {'#Max':max_points}'Àº #Max¸¦ Å°·Î ÇÏ´Â »õ·Î¿î »çÀüÀ» »ý¼ºÇÑ´Ù. ±×¸®°í ±× °ªÀ» [25,25,50,25,100] À¸·Î ¼³Á¤ÇÑ´Ù. (±×°ÍÀÌ ÇÒ´çÀÌ ÀÌ·ç¾î Áú¶§ÀÇ 'max_points' °ªµéÀ̹ǷÎ) (¾ËÆĺª¹®ÀÚÁß ¾î¶² °Íº¸´Ùµµ ¾Õ¿¡ Á¤·ÄµÇ¹Ç·Î ³ª´Â Å°'#Max'¸¦ »ç¿ëÇÑ´Ù.) ´ÙÀ½À¸·Î print_menu °¡ Á¤ÀÇ µÈ´Ù. ´ÙÀ½À¸·Î print_all_grades ÇÔ¼ö°¡ ÀÌ ¶óÀο¡¼­ Á¤ÀǵȴÙ:
def print_all_grades():
        print '\t',
        for i in range(len(assignments)):
            print assignments[i],'\t',
        print
        keys = students.keys()
        keys.sort()
        for x in keys:
            print x,'\t',
            grades = students[x]
            print_grades(grades)
¸ÕÀú 'keys = students.keys() '¿¡¼­ keysÇÔ¼ö¸¦ °¡Áö°í students »çÀüÀ¸·Î ºÎÅÍ ¾î¶»°Ô Å°¸¦ ȹµæÇÏ´ÂÁö ÁÖ¸ñÇ϶ó. keys´Â ÇϳªÀÇ ¸®½ºÆ®ÀÌ´Ù ±×·¡¼­ ¸®½ºÆ®¸¦ À§ÇÑ ¸ðµç ÇÔ¼ö°¡ ±×°Í¿¡ »ç¿ëµÉ ¼ö ÀÖ´Ù. ´ÙÀ½À¸·Î ±× Å°´Â ¸®½ºÆ®À̹ǷΠ´ÙÀ½ÀÇ ¶óÀÎ 'keys.sort()'¿¡¼­ Á¤·ÄµÈ´Ù. 'for'¹®Àº ¸ðµç Å°µéÀ» ¹æ¹®Çϴµ¥ »ç¿ëµÈ´Ù. ÇгâÀº »çÀü¾È¿¡¼­ ¸®½ºÆ®ÀÇ ÇüÅ·ΠÀúÀåµÇ¾î¼­ ÇÒ´ç¹® ''´Â grades ¿¡°Ô Å° x ¿¡ ÀúÀåµÈ ¸®½ºÆ®¸¦ ³Ñ°Ü ÁÙ ¼ö ÀÖ´Ù. ÇÔ¼ö print_grades´Â ´ÜÁö ¸®½ºÆ®¸¦ Ãâ·ÂÇϱ⸸ ÇÑ´Ù ±×¸®°í ¸î ¶óÀÎ µÚ¿¡¼­ Á¤ÀÇ µÈ´Ù.

ÇÁ·Î±×·¥ÀÇ ÀÌÈÄÀÇ ¶óÀεéÀº ¸Þ´ºÀÇ ´Ù¾çÇÑ ¼±ÅûçÇ×µéÀ» ±¸ÇöÇÑ´Ù. ¶óÀÎ 'students[name] = [0]*len(max_points)'´Â ÇÑ ÇлýÀ» ±×µéÀÇ À̸§À¸·Î ÀÌ·ç¾îÁø Å°¿¡ Ãß°¡ÇÑ´Ù. ´ÙÀ½ÀÇ Ç¥±â¹ý '[0]*len(max_points)'Àº ´Ü¼øÈ÷ 0 À¸·Î ÀÌ·ç¾îÁø ¹è¿­ Çϳª¸¦ »ý¼ºÇϴµ¥ max_points ¸®½ºÆ®¿Í °°Àº ±æÀÌÀÌ´Ù.

Á¦°ÅµÈ ÇлýÀÇ Ç׸ñÀº ÀüÈ­¹øÈ£ºÎ ¿¹¿¡¼­¿Í ºñ½ÁÇÏ°Ô ´ÜÁö ÇлýÀ» Áö¿ì±â¸¸ ÇÑ´Ù. Çг⠼±Åà ·¹ÄÚµå´Â ¾à°£Àº Á» ´õ º¹ÀâÇÏ´Ù. ÇгâÀº 'grades = students[name]'¶óÀο¡¼­ °Ë»öµÇ¾î¼­ nameÇлýÀÇ Çг⿡ ´ëÇÑ ÂüÁ¶¸¦ ȹµæÇÑ´Ù. ±×¸®°í ³ª¼­ ÇгâÀº 'grades[which] = grade'¶óÀο¡¼­ ±â·ÏµÈ´Ù. ¿©·¯ºÐÀº 'grades' °¡ ´Ù½Ã Çлý »çÀüÀ¸·Î µÇ µ¹·Á ÀÔ·Â µÇÁö ¾Ê´Â´Ù´Â °ÍÀ» ÁÖ¸ñÇ϶ó.( ´ÙÀ½°ú °°ÀÌ 'students[name] = grades' ºÒ°¡ÀÌ´Ù.). ±× ¹®ÀåÀÌ À߸øµÈ ÀÌÀ¯´Â grades °¡ ½ÇÁ¦·Î´Â students[name] ÀÇ ´Ù¸¥ À̸§À̱⠶§¹®ÀÌ°í ±×·¯¹Ç·Î grades¸¦ º¯°æÇÏ´Â °ÍÀº 'student[name]'À» º¯°æÇÏ´Â °ÍÀ̱⠶§¹®ÀÌ´Ù.

»çÀüÀº Å°¿Í °ªµéÀ» ¿¬°áÇÏ´Â ½¬¿î ¹æ¹ýÀ» Á¦°øÇØ ÁØ´Ù. ÀÌ°ÍÀº ¿©·¯°¡ÁöÀÇ Å°¿¡ ¿¬°áµÇ¾î ÀÖ´Â µ¥ÀÌŸ¸¦ ½±°Ô ÃßÀûÇϴµ¥ »ç¿ëµÉ ¼ö ÀÖ´Ù.


next up previous contents
Next: ¸ðµâ »ç¿ëÇϱâ Up: ÀϹÝÀÎÀ» À§ÇÑ ÆÄÀ̽ã Áöħ¼­ Previous: ºÎ¿ï Ç¥Çö½Ä   ¸ñ Â÷
Josh Cogliati jjc@iname.com