Python programming

(23.02.01.)Python ํ”„๋กœ๊ทธ๋ž˜๋ฐ : Tuple(ํŠœํ”Œ) & Dictionary(๋”•์…”๋„ˆ๋ฆฌ)

ํ”„๋กœ๊ทธ๋ž˜๋จธ ์˜ค์›” 2023. 2. 6.

Tuple

 

 

 


 

 

Dictionary

 


List - Dictionary ๊ตฌ์กฐ๋ฅผ ์ด์šฉํ•œ ์‚ฌ์›๊ด€๋ฆฌ ํ”„๋กœ๊ทธ๋žจ CRUD

 

 

 

1
2
3
4
def show_menu():
    menu = input("๋ชฉ๋ก(s), ์ถ”๊ฐ€(a), ๊ฒ€์ƒ‰(f), ์ˆ˜์ •(u), ์‚ญ์ œ(d), ์ข…๋ฃŒ(x)")
    menu = menu.strip()
    return menu
cs

 

 

 

1
2
3
4
5
6
7
8
9
10
11
def add():
    str_emp = input("๋ฒˆํ˜ธ,์ด๋ฆ„,ํ•ธ๋“œํฐ๋ฒˆํ˜ธ ์ž…๋ ฅ: ").strip()
    cnt = len(str_emp.split())
    if cnt < 3 :
        print("3๊ฐœ์˜ ๊ฐ’์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค")
        return
    num,name,phone = str_emp.split()
    if num is None or name is None or phone is None:
        print(data,'ํ•„์ˆ˜ํ•ญ๋ชฉ์ด ์ž…๋ ฅ๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค')
        return None
    return ["eno":num,"ename":name,"phone":phone]
cs

 

 

 

1
2
3
4
5
emp_list = []
 
def show_list():
    for emp in emp_list:
        print(f'{emp["eno"],emp["ename"],emp["phone"]}')
cs

 

 

 

1
2
3
4
5
6
7
8
9
def find():
    eno = input("๊ฒ€์ƒ‰ํ•  ์‚ฌ์›๋ฒˆํ˜ธ").strip()
    found = False
    for emp in emp_list:
        if emp["eno"]==eno:
            print(f'{emp["eno"],emp["ename"],emp["phone"]}')
            found = True
        if not found:
            print(f'{eno}๋ฒˆ ์‚ฌ์›์ •๋ณด๋Š” ์—†์Šต๋‹ˆ๋‹ค')
cs

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def update():
    upinfo = input("์ˆ˜์ •ํ•  ์‚ฌ๋ฒˆ ์ „ํ™”๋ฒˆํ˜ธ: ").strip()
    snum,phone = upinfo.split()
    if snum is None or phone is None:
        print("์‚ฌ๋ฒˆ์ด๋‚˜ ์ „ํ™”๋ฒˆํ˜ธ ๋ˆ„๋ฝ๋จ")
        return False
    updated = False
    for i in range(len(emp_list)):
        if snum in emp_list[i]["eno"]:
            emp_list[i]["phone"= phone
            print("์‚ฌ์›์ •๋ณด ์ˆ˜์ • ์„ฑ๊ณต")
            updated = True
    if not updated:
        print("์‚ฌ์›์ •๋ณด ์ˆ˜์ • ์‹คํŒจ")
    return updated
cs

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
def delete():
    snum = input("์‚ญ์ œํ•  ์‚ฌ์›๋ฒˆํ˜ธ: ").strip()
    deleted = False
    for emp in emp_list:
        if snum == emp["eno"]:
            emp_list.remove(emp)
            deleted = True
            print("์‚ญ์ œ์„ฑ๊ณต")
            break
    if not deleted:
        print("์‚ญ์ œ์‹คํŒจ")
        return deleted
cs

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
while True :
    menu = show_menu()
    if(menu =='x') :
        break
    elif(menu == 'a') :
        emp = add()
        if emp is not None:
            emp_list.append(emp)
            print('์‚ฌ์›์ •๋ณด ์ถ”๊ฐ€ ์™„๋ฃŒ')
        else:
            print("์ถ”๊ฐ€์‹คํŒจ")
    elif(menu == 's') :
        show_list()
    elif(menu == 'f') :
        find()
    elif(menu == 'u') :
        update()
    elif(menu == 'd') :
        delete()
    else:
        print("๋ฉ”๋‰ด์ž…๋ ฅ ์˜ค๋ฅ˜")
print("ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ")
cs

 

 

 

 

 

 

๋Œ“๊ธ€