在输入“END”之前我无法看到结果,然后它会在列表中打印所有结果。我希望它在我输入一个或多个字母并按 Enter 键后打印每个结果。我知道我只需要停止在附加列表中建立多个单词,但我无法弄清楚。我当前的代码是...
当前代码
def information_gathering_phase():
while True:
word = input("Enter a word from the letters 'b', 'd', 'e', 'L', 'S' and/or 'M' (or type 'END' to stop program): ")
if word == 'END':
return None
elif not all(letter in 'bdeLMS' for letter in word):
print("Invalid word! Please try again...")
else:
return word
def grid_7x7(word):
letters = {
'b': [' ', ' * ', ' * ', ' ***** ', ' * *', ' * *', ' *****'],
'd': [' ', ' *', ' *', ' *****', ' * *', ' * *', ' ***** '],
'e': [' ', ' **** ', ' * *', ' ***** ', ' * ', ' * *', ' **** '],
'L': [' ', ' * ', ' * ', ' * ', ' * ', ' * ', ' ***** '],
'S': [' ', ' *** ', ' * * ', ' * ', ' * ', ' * * ', ' ** '],
'M': [' ', ' * *', ' ** **', ' * ** *', ' * *', ' * *', ' * *']
}
for i in range(7):
for letter in word:
print(letters[letter][i],end='')
print()
def main():
print("Information Gathering phase:")
words = []
while True:
word = information_gathering_phase()
if word is None:
break
words.append(word)
print("Information Presenting phase:")
for word in words:
grid_7x7(word)
print()
if __name__ == '__main__':
main()
结果...
信息收集阶段:(所有显示同时输入有效字母或有效字母组合) 无效词!请重试...(当输入一个或多个无效字母时) 信息呈现阶段:(输入“END”并按 Enter 后显示)
Information Gathering phase:
Invalid word! Please try again...
Information Presenting phase:
* ****
* * *
***** *****
* * *
* * * *
***** ****
* *** * *
* * * ** **
* * * ** *
* * * *
* * * * *
***** ** * *
如果我理解正确,您希望在输入每个输入字符串后立即对其进行转换。为此,您不需要任何输入列表,而只需要最新的输入列表。您还需要在请求下一个输入之前打印结果,因此两者都需要成为同一循环的一部分。这是一种方法:
def information_gathering_phase():
while True:
word = input("Enter a word from the letters 'b', 'd', 'e', 'L', 'S' and/or 'M' (or type 'END' to stop program): ")
if word == 'END':
return None
elif not all(letter in 'bdeLMS' for letter in word):
print("Invalid word! Please try again...")
else:
return word
def grid_7x7(word):
letters = {
'b': [' ', ' * ', ' * ', ' ***** ', ' * *', ' * *', ' *****'],
'd': [' ', ' *', ' *', ' *****', ' * *', ' * *', ' ***** '],
'e': [' ', ' **** ', ' * *', ' ***** ', ' * ', ' * *', ' **** '],
'L': [' ', ' * ', ' * ', ' * ', ' * ', ' * ', ' ***** '],
'S': [' ', ' *** ', ' * * ', ' * ', ' * ', ' * * ', ' ** '],
'M': [' ', ' * *', ' ** **', ' * ** *', ' * *', ' * *', ' * *']
}
for i in range(7):
for letter in word:
print(letters[letter][i],end='')
print()
def main():
while True:
word = information_gathering_phase()
if word is None:
break
else:
grid_7x7(word)
if __name__ == '__main__':
main()
我只是稍微改变了你的主要功能。剩下的我觉得都不错