Entering a phone book with Python

Asked 2 years ago, Updated 2 years ago, 38 views

Hello, I'm a beginner at Python.

Kim Cheol-soo 010-1234-5678
Park Young-hee 010-9876-5432

I'd like to enter a phone book like this.

I want to program it in advance, and in Python Shell, just type in your name and phone number like above and create a phone book on your own. I think we can use the dictionary. How should I organize the program so that a phone book can be created by itself even if I type it as simple as above?

It's only been 2 weeks since I started Python, so it's really hard ㅠ<

python phone-number

2022-09-22 17:55

1 Answers

The code below is based on the assumption that the input is accurate.

def make_phonebook():
     phone_book ={}

     print("Please enter your name and phone number.")
     print (e.g. Hong Gil-dong 1223-4567-8901")
    print ("Please enter quit for input interrupt")

    while 1:
        name_phone = input ('input:')

        if name_phone == 'quit':break
        phone_book[name_phone.split(' ')[0]] = name_phone.split(' ')[1]

    print(phone_book)

>>> make_phonebook()
Please enter your name and phone number.
Example: Hong Gil-dong 1223-4567-8901
Do you enter quit to abort?
Input: Hong Gil-dong 1223-4567-8901
Input: Kim Gil-dong 2432-424-4242
Input: Kang Gil-dong 9953-462-5456
input:quit
{'Hong Gil-dong': '1223-4567-8901', 'Kim Gil-dong': '2432-424-4242', 'Gang Gil-dong': '9953-462-5456'}


2022-09-22 17:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.