【问题标题】:How to add a statement that checks for numbers or punctuation in a string如何添加检查字符串中的数字或标点符号的语句
【发布时间】:2016-06-06 08:18:21
【问题描述】:

我目前有一个将英语翻译成莫尔斯电码的代码。它工作正常,但我想添加另一个参数,允许程序在输入数字或标点符号时打印###

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

我正在考虑添加另一个类似于

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

但我不知道如何让它只检查字符串中的数字和标点符号。例如,hi! 将打印 .....###。 谢谢

【问题讨论】:

    标签: list python-3.x if-statement


    【解决方案1】:

    这是一个简化版本。我把你的 translation_table list 变成了 dict。我也在使用isalpha() 方法来检查给定的字符是否是字母。

    translationDict = {'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':'--..'}
    
    english = input('Enter a sentence to be translated: ')  
    
    for character in english:
        if character is ' ':
            print(' ', end='')
        elif character.isalpha():
            print(translationDict[character], end='')
        else:
            print('###', end='')
    

    这是使用列表的类似实现:

    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","--.."]]
    
    english = input('Enter a sentence to be translated: ')  
    
    for character in english:
        if character is ' ':
            print(' ', end='')
        elif character.isalpha():
            for i in translation_table:
                if character in i:
                    print(i[1], end ='')
        else:
            print('###', end='')
    

    【讨论】:

    • 我应该提出我最初的问题,我们不允许使用字典,哎呀!是的,这可能是最好的做事方式。
    • 我在答案中添加了一个列表示例。
    猜你喜欢
    • 1970-01-01
    • 2013-10-03
    • 2015-12-02
    • 2011-08-11
    • 1970-01-01
    • 2015-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多