# MorseCodes.py Copyright (c) Kari Laitinen # http://www.naturalprogramming.com # 2006-05-20 File created. # 2022-12-27 Converted to Python 3. # List method index() raises ValueError if the specified item # is not found on the list. Otherwise, it returns the index # of the first occurrence of the specified item. list_of_morse_codes = \ [ "A", ".-", "B", "-...", "C", "-.-.", "D", "-..", "E", ".", "F", "..-.", "G", " --.", "H", "....", "I", "..", "J", ".---", "K", "-.-", "L", ".-..", "M", "--", "N", "-.", "O", "---", "P", ".--.", "Q", "--.-", "R", ".-.", "S", "...", "T", "-", "U", "..-", "V", "...-", "W", ".--", "X", "-..-","Y", " -.--", "Z", "--..", "1", ".----","2", "..---","3", "...--","4","....-", "5", ".....","6", "-....","7", "--...","8", "---..","9","----.", "0", "-----"," ", " " ] print( "\n Type in your name: ", end="" ) given_name = input().upper() print( "\n Your name in Morse codes is: \n" ) for character_in_name in given_name : try : index_of_character_in_list = \ list_of_morse_codes.index( character_in_name ) print( " " + \ list_of_morse_codes[ index_of_character_in_list + 1 ], end="" ) except ValueError : print( "\n Un-morse-codable character: " + character_in_name ) print( "\n" )