Test/Python(20220101~)

NATO 음성 알파벳 예제의 예외처리

kiostory 2022. 9. 18. 19:08

nato_phonetic_alphabet.csv
0.00MB

import pandas

#TODO 1. using csv file, Create a dictionary in this format like:
#{"A": "Alfa", "B": "Bravo"}

data = pandas.read_csv("./nato_phonetic_alphabet.csv")
data_dict_comprehension = { row.letter:row.code
    for (index,row) in data.iterrows()
    }

#TODO 2. Create a list of the phonetic code words from a word that the user inputs.

def phonetic():
    word = input("Enter a word: ").upper()
    try:
        phonetic_word_lists = [ data_dict_comprehension[letter] for letter in word ]
    except KeyError:
        print("Sorry, only letters in the alphabet please.  ")
        phonetic()
    else:
        print(phonetic_word_lists)

phonetic()

-------------------------------

Enter a word: 123
Sorry, only letters in the alphabet please.  
Enter a word: 1w
Sorry, only letters in the alphabet please.  
Enter a word: fdsaf5
Sorry, only letters in the alphabet please.  
Enter a word: i love you
Sorry, only letters in the alphabet please.  
Enter a word: iloveyou
['India', 'Lima', 'Oscar', 'Victor', 'Echo', 'Yankee', 'Oscar', 'Uniform']

Process finished with exit code 0