Python dasturlash tilida ro’yxatlar va kortejlar bilan bir qatorda lug’atlar (dictinary) deb nomlanuvchi berilganlarning ichki tuzilmasi mavjud. Lug’atlar ham xuddi ro’yxatlar kabi elementlar to’plamini saqlaydi. Lug’atdagi har bir element unikal kalitga ega bo’ladi va unga biror bir qiymat bog’lanadi.
Kompleks (murakkab) lug’atlar. Lug’atlar faqatgina int, str, float, bool kabi oddiy turlarga oid berilganlardangina emas, balki list, tuple, set, dict kabi murakkab tuzulmaviy berilganlardan ham tashkil topishi mumkin:
Masala. Telefon raqamlarni va uning egalarnining F.I.O, Manzil, elektron pochtalari, o‘zida saqlaydigan phonenumbers_dict lug‘ati yaratilsin, hamda quyidagi amalar bajarilsin. Ko‘rsatma, kalit sifatida telefon raqami olinsin, hamda, murkkab lug‘atlardan foydalanilsin. Quyidagi ammalarni bajaring:
Qo‘shish, o‘chirish, tahrirlash;
Telefon ramlarni va uning egasining F.I.O chiqaradigan funkisya tuzilsin;
Kiritilgan Familiya bo‘yicha telefon raqamlari chiqrilsin;
Dasturni tuzish jarayonida istisno hollatlardan foydalanilsin;
Manzilning satr ostisi kiritilganda, shu satr ositiga ega bo‘lgan raqamlar chiqarilsin.
Masala kodi 1
# Bo'sh Lug'at yaratish phonenumbers_dict = {} # Func adding new number def AddNumber(): # Calling phonenumbers_dict for local global phonenumbers_dict # Creat new phone number newPhone = {input("Enter new phone number: ") : { "fio" : { "fam" : input("Enter last name: "), "ism" : input("Enter first name: "), "middle" : input("Enter midle name: ") }, "address" : { "place" : input("Enter address: "), "email" : input("Enter e-mail: ") } }}
Masala kodi 2
# Adding the number note the new number phonenumbers_dict.update(newPhone) # Func: deleting the needed number def DeleteNumber(): # Calling phonenumbers_dict for local global phonenumbers_dict # Input the numbers data for deleting pass # Func: Editing the needed number def EditNumber(): # Calling phonenumbers_dict for local global phonenumbers_dict # Input the numbers data for deleting pass
Masala kodi 3
# Func: Editing the needed number def PrintNumbers(): # Calling phonenumbers_dict for local global phonenumbers_dict for item in phonenumbers_dict: print("Phone number: ", item) print("" * 20, end='') print("F.I.O.: ", item["fio"]["fam"], " | ", item["fio"]["ism"], " | ", item["fio"]["midle"]) print("Address: ", item["address"]["place"], " | ", item["address"]["email"])
Masala kodi 4
def SearchByFirstName(): # Calling phonenumbers_dict for local global phonenumbers_dict def SearchByAddress(): # Calling phonenumbers_dict for local global phonenumbers_dict while True: print("Buyruqlar:") print("1-Yangi raqam qo'shish") print("2-Tarirlash") print("3-O'chirish") print("4-Chop qilish") print("5-Familiya bo'yicha qidirish") print("6-Manzil bo'yicha qidirish") print("0-Chiqish")
Masala kodi 5
# Buyruqni kiritish command = int(input("Buyruq raqamini kiriting: ")) # Birinchi buyroq funksiyasiga murojat qilish if command == 1: AddNumber() # Keyingi if shartlarni qaramaslik uchun continue # 2-buyroq funksiyasiga murojat qilish if command == 2: EditNumber() continue # 3-buyroq funksiyasiga murojat qilish if command == 3: DeleteNumber() continue
Masala kodi 6
# 4-buyroq funksiyasiga murojat qilish if command == 4: continue # 5-buyroq funksiyasiga murojat qilish if command == 5: SearchByFirstName() continue # 6-buyroq funksiyasiga murojat qilish if command == 6: SearchByAddress() continue # 0-buyroq funksiyasiga murojat qilish if command == 0: # Exit while break
Vazifalar
Yuqoridagi masaladagi quyidagi asosiy funksiyalarning kodi yozilsin va testlar o‘tkazilsin va hamma kodlar istisno holatlari ko‘rinishiga o‘tkazilsin:
def SearchByFirstName(): # Calling phonenumbers_dict for local global phonenumbers_dict def SearchByAddress(): # Calling phonenumbers_dict for local global phonenumbers_dict