我正在尝试编写一个程序,其中控制台设置为打印字母“A,B,N,O,T,Y”的任意组合。 该程序必须首先以一条输入消息开始,其中输入有 3 个结果:
任何帮助将不胜感激:)
代码:
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()
您的代码中存在多个问题。让我们来解决它们:
myConsole()
函数应返回有效的输入单词。validLetters()
功能未集成到主代码中。__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”以终止循环。