Created
October 12, 2022 18:44
-
-
Save dbosk/6244091578301a8926d4c29031ada284 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- search.py 2022-10-12 20:41:07.597133491 +0200 | |
+++ search-contacts.py 2022-10-12 20:36:14.015572596 +0200 | |
@@ -23,6 +23,66 @@ | |
return phonenum | |
+def input_full_name(prompt="Ange fullständigt namn."): | |
+ """Ber användaren mata in namn, returnerar (förnamn, efternamn).""" | |
+ print(prompt) | |
+ | |
+ firstname = input("Ange förnamn: ") | |
+ while not firstname: | |
+ firstname = input("Ange förnamn: ") | |
+ | |
+ lastname = input(f"Ange efternamn för {firstname}: ") | |
+ | |
+ capitalize = input("Stavar personen med inledande stor bokstav? [ja/nej] ") | |
+ if capitalize.casefold() == "ja": | |
+ firstname = firstname.capitalize() | |
+ lastname = lastname.capitalize() | |
+ | |
+ return firstname, lastname | |
+ | |
+def input_contact(prompt=""): | |
+ """ | |
+ Låter användaren mata in kontaktinfo om en kontakt, | |
+ returnerar en dictionary med nycklarna: | |
+ - first_name | |
+ - last_name | |
+ - nickname | |
+ - phone_number | |
+ """ | |
+ contact_info = {} | |
+ | |
+ first, last = input_full_name() | |
+ contact_info["first_name"] = first | |
+ contact_info["last_name"] = last | |
+ | |
+ nickname = input(f"Smeknamn för {first}: ") | |
+ contact_info["nickname"] = nickname | |
+ | |
+ if nickname: | |
+ phonenum = input_phonenum(f"Ange telefonnummer för {nickname}: ") | |
+ else: | |
+ phonenum = input_phonenum(f"Ange telefonnummer för {first}: ") | |
+ | |
+ contact_info["phone_number"] = phonenum | |
+ | |
+ return contact_info | |
+ | |
+def print_contact_info(contact_info): | |
+ """ | |
+ Tar en dictionary med kontaktinfo och skriver ut på skärmen, | |
+ har nycklarna: | |
+ - first_name | |
+ - last_name | |
+ - nickname | |
+ - phone_number | |
+ """ | |
+ print(f"{contact_info['nickname']} " | |
+ f"({contact_info['first_name']} {contact_info['last_name']}):") | |
+ print(f"Phone: {contact_info['phone_number']}") | |
+ print(f"Email: {contact_info['email']}") | |
+ | |
def fill_phonbook(): | |
""" | |
Ber användaren att fylla telefonboken. | |
@@ -32,9 +92,8 @@ | |
name = input_name("Ange namn för en ny kontakt: ") | |
while name: | |
- number = input_phonenum( | |
- f"Ange telefonnummer för {name.capitalize()}: ") | |
- phonebook[name] = number | |
+ phonebook[name] = input_contact( | |
+ f"Ange kontaktinformation för {name.capitalize()}: ") | |
print() | |
name = input_name("Ange namn för en ny kontakt: ") | |
@@ -46,7 +105,7 @@ | |
name = input_name() | |
while name: | |
try: | |
- print(f"{name.capitalize()} har telefonnummer {phonebook[name]}.") | |
+ print_contact_info(phonebook[name]) | |
except KeyError: | |
print(f"Hittade INTE {name.capitalize()} i telefonboken.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment