我试图在Python 3中创建一个糟糕的字符过滤器

时间:2016-11-03 00:53:51

标签: python

我正在尝试设置一个过滤器,当输入除A,C,E,T,O或P以外的任何字符时,该过滤器将打印出“输入错误”消息。出于某种原因,我无法绕过我需要做的事情以确保在输入GOOD输入时实际发生findLetter()函数。任何人都可以帮我解决我在过滤器中出错的问题吗?

reset = True;
while ( reset == True ):

#A C E T O P
    def letterA():
        print('   *   ');
        print('  * *  ');
        print(' ***** ');   
        print('*     *');
        print('       ');   
    def letterC():
        print('*****');
        print('*    ');
        print('*    ');
        print('*    ');
        print('*****'); 
        print('     '); 
    def letterE():
        print('****');
        print('*   ');
        print('*** ');
        print('*   ');
        print('****');
        print('    ');  
    def letterT():
        print('*****');
        print('  *  ');
        print('  *  ');
        print('  *  '); 
        print('     '); 
    def letterO():
        print('*****');
        print('*   *');
        print('*   *');
        print('*****'); 
        print('     '); 
    def letterP():
        print('*****');
        print('*   *');
        print('*****');
        print('*    ');
        print('*    ');
        print('     '); 
    def findLetter():
        for x in range(len(temp)):
            if ( temp[x] == 'a' or temp[x] == 'A' ):
                letterA();
            elif ( temp[x] == 'c' or temp[x] == 'C' ):
                letterC();
            elif ( temp[x] == 'e' or temp[x] == 'E' ):
                letterE();
            elif ( temp[x] == 'p' or temp[x] == 'P' ):
                letterP();
            elif ( temp[x] == 'o' or temp[x] == 'O' ):
                letterO();
            elif ( temp[x] == 't' or temp[x] == 'T' ):
                letterT();

    def filter(y):  
        for x in range( len(y) ): #bad input filter
            while ( ( y[x] != "a" ) and ( y[x] != "c" ) and ( y[x] != "e" ) and ( y[x] != "p" ) and ( y[x] != "o" ) and ( y[x] != "t" ) and ( y[x] != "A" ) and ( y[x] != "C" ) and ( y[x] != "E" ) and ( y[x] != "P" ) and ( y[x] != "O" ) and ( y[x] != "T" ) ):
                if ( ( y[x] != "a" ) and ( y[x] != "c" ) and ( y[x] != "e" ) and ( y[x] != "p" ) and ( y[x] != "o" ) and ( y[x] != "t" ) and ( y[x] != "A" ) and ( y[x] != "C" ) and ( y[x] != "E" ) and ( y[x] != "P" ) and ( y[x] != "O" ) and ( y[x] != "T" ) ):
                    y = input( 'Please enter a valid input: A, C, E, P, O, T : ' );
                    userInput = y;
                    temp = y;

    userInput = input('Please input any word using any of the following 6 letters: A, C, E, P, O, T : ');           
    temp = userInput;   
    filter(userInput);      
    findLetter();

    repeat = input('Would you like to spell more words? (y/n): ');
    while ( repeat != 'y' and repeat != 'n' ):
        repeat = input('Would you like to spell more words? (y/n): ');      
    if ( repeat == 'n' ):
        reset = False;

2 个答案:

答案 0 :(得分:1)

将过滤功能切换为:

def filter(y):  
    valid_letters = ['A', 'a', 'C', 'c', 'E', 'e', 'P', 'p', 'O', 'o', 'T', 't']
        if not any(valid in y for valid in valid_letters):
            print('Invalid input')
            y = input( 'Please enter a valid input: A, C, E, P, O, T : ' );
            userInput = y;
            temp = y;
            filter(y)

我所做的是创建一个名为valid_letters的有效字符列表,然后检查userInput中的字符是否不在有效字符列表中:

if not any(valid in y for valid in valid_letters):

您的findLetter功能似乎很好;我没有办法改变这一点。

(另外,在Python中,你不必用分号显示一行的结尾:-D)

答案 1 :(得分:0)

我已编辑此答案以考虑循环,它比另一个答案更详细(这是一个很好的答案!)。希望在评论中描述:

allowed_letters

在检查{{1}}中的字母时,将字母转换为大写字母,您不必包含大写和小写版本。