创建一个在 8x8 网格中打印特定字母的 Python 程序

问题描述 投票:0回答:1

我正在尝试编写一个程序,其中控制台设置为打印字母“A,B,N,O,T,Y”的任意组合。 该程序必须首先以一条输入消息开始,其中输入有 3 个结果:

  1. 如果输入了错误的字符,则返回错误消息
  2. 如果输入任何可接受的字母组合,请并排打印字母并重新要求输入另一个
  3. 以不区分大小写的“End”结束程序 这是我到目前为止的代码。我被 myConsole() 困住了,不允许程序开始定义字母。我还希望程序在输入正确的情况下立即打印单词并重新要求另一个输入。这段代码正确运行后,我只能输入一个字母,并在输入“End”后打印。

任何帮助将不胜感激:)

代码:

    def myConsole():
      userInput=""
      while(True):
        userInput = input("Enter New Word Here (Type 'END' to close) >>> ")
        if userInput.lower() == 'end':
          return
          print(eval(userInput))
        #elif not all(letter in 'abnoty' for letter in word):
          #print('Please Enter A Valid Word (i.e. Words Only Containing the Letters "A", "B", "N", "O", "T" and "Y")')
        else:
          #return word
          print('Please Enter A Valid Word (i.e. Words Only Containing the Letters "A", "B", "N", "O", "T" and "Y")')

    myConsole()
    print('End of Program, Goodbye')

    def validLetters (word):
        letters = {
            'a': [' ', '   *   ', '  * *  ', ' *   * ', '*******', '*     *', '*     *', '*     *'],
            'b': [' ', '****** ', '*     *', '*     *', '****** ', '*     *', '*     *', '****** '],
            'n': [' ', '*     *', '**    *', '* *   *', '*  *  *', '*   * *', '*    **', '*     *'],
            'o': [' ', '  ***  ', ' *   * ', '*     *', '*     *', '*     *', ' *   * ', '  ***  '],
            't': [' ', '*******', '   *   ', '   *   ', '   *   ', '   *   ', '   *   ', '   *   '],
            'y': [' ', '*     *', ' *   * ', '  * *  ', '   *   ', '   *   ', '   *   ', '   *   ']
}

        for i in range(8):
            for letter in word:
                print(letters[letter][i], end=' ')
            print()

    def output ():
      words = []
      while (True):
        word = myConsole()
        if word is None:
          break
        words.append (word)

      for word in words:
        print_grid (word)
        print()

    if __name__ == '__output__':
      output()
python loops printing console
1个回答
0
投票

您的代码中存在多个问题。让我们来解决它们:

  1. myConsole()
    函数应返回有效的输入单词。
  2. 对不正确字符的验证已被注释掉。
  3. validLetters()
    功能未集成到主代码中。
  4. 不正确使用
    __name__ == '__output__'
    。应该是
    __name__ == '__main__'

这是一个重构版本:

def myConsole():
    while True:
        userInput = input("Enter New Word Here (Type 'END' to close) >>> ")
        if userInput.lower() == 'end':
            return None
        elif all(letter.lower() in 'abnoty' for letter in userInput):
            print(userInput)
            return userInput
        else:
            print('Please Enter A Valid Word')

def validLetters(word):
    letters = {
        'a': [' ', '   *   ', '  * *  ', ' *   * ', '*******', '*     *', '*     *', '*     *'],
        'b': [' ', '****** ', '*     *', '*     *', '****** ', '*     *', '*     *', '****** '],
        'n': [' ', '*     *', '**    *', '* *   *', '*  *  *', '*   * *', '*    **', '*     *'],
        'o': [' ', '  ***  ', ' *   * ', '*     *', '*     *', '*     *', ' *   * ', '  ***  '],
        't': [' ', '*******', '   *   ', '   *   ', '   *   ', '   *   ', '   *   ', '   *   '],
        'y': [' ', '*     *', ' *   * ', '  * *  ', '   *   ', '   *   ', '   *   ', '   *   ']
    }

    for i in range(8):
        for letter in word.lower():
            print(letters[letter][i], end=' ')
        print()

if __name__ == '__main__':
    while True:
        word = myConsole()
        if word is None:
            break
        validLetters(word)
    print('End of Program, Goodbye')

运行此代码。它现在应该验证输入字符,立即打印它们,并处理“End”以终止循环。

© www.soinside.com 2019 - 2024. All rights reserved.