mardi 23 février 2016

How to add a statement that checks for numbers or punctuation in a string

I currently have a code that translates english into morse code. It works fine but I want to add another argument that allows the program to print ### whenver a number or punctuation is inputted.

translation_table = [["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","--.."]]

word_index = 0
english = input("Enter a sentence to be translated(*** to end): ")
words = list(english)
translated_sentence = ""
while word_index < len(words):
    if english == "***":
        print("Program has ended")
        break
    translation_index = 0
    while translation_index < len(translation_table):
        if translation_table[translation_index][0] == words[word_index]:
           translated_sentence = translated_sentence + translation_table[translation_index][1]
           translation_index = 27
        elif words[word_index] == " ":
            translated_sentence = translated_sentence + "   "
            translation_index = 27
        else :
            translation_index = translation_index + 1

I was thinking of adding another elif line that was something like

elif words[word_index][0] == : 
        translated_sentence = translated_sentence + "###"
            translation_index = 27

but I have no idea how to make it check for only numbers and punctuation in the string. For example, hi! would print .....###. Thank you

Aucun commentaire:

Enregistrer un commentaire