我似乎无法让程序继续运行,直到我输入“停止”命令。我下面有这个新代码,可以按照我的需要将字母打印为星形图案,但我需要它继续下去,直到我输入“停止”。
def pattern():
for i in range(len(name)):
if name [i] =="B":
print_B = [[" " for i in range (5)] for j in range (5)]
for row in range(5):
for col in range(5):
if (col==0) or (col==4 and (row!=0 and row!=2 and row!=4)) or ((row==0 or row==2 or row==4) and (col>0 and col<4)):
print_B[row] [col] = "*"
list2.append(print_B)
elif name [i] =="A":
print_A = [[" " for i in range (5)] for j in range (5)]
for row in range(5):
for col in range(5):
if (row==0 and 1<col<3)or((row==1 and 0<col<2)or(row==1 and 2<col<4))or(row==2 and 0<=col)or((row==3 or row==4) and (0<=col<1 or 3<col>=4)):
print_A[row] [col] = "*"
list2.append(print_A)
elif name [i] =="L":
print_L = [[" " for i in range (5)] for j in range (5)]
for row in range(5):
for col in range(5):
if ((col==0) and row<=4) or ((row==4) and (0<col<=4)):
print_L[row] [col] = "*"
list2.append(print_L)
else:
print("INVALID")
return list2
name = input("Enter the name:")
list2 = []
list3 = pattern()
for i in range (5):
for j in range(len(list3)):
for k in range(5):
print(list3[j][i][k],end=" ")
print(end=" ")
print ()
我知道这是相当简单的事情,但我希望能帮助解决这个问题,并且如果可能的话,我希望对添加的每段代码的作用有一个基本解释。
谢谢。
我知道我需要从...开始
def information_gathering_phase():
while True:
word = input("Enter a word from the letters 'B', 'A' and/or 'L' (or type 'stop' to stop the program): ")
if word == 'stop' or word == 'STOP':
return None
elif not all(letter in 'BAL' for letter in word):
print("Invalid word! Please try again...")
else:
return word
并以类似...的方式结束
def main():
while True:
word = information_gathering_phase()
if word is None:
print("Thanks for trying my program...Bye for now!")
break
else:
??? I don't know - ? some return statement
if __name__ == '__main__':
main()
但我似乎无法将新旧融合在一起。
您需要一个 while 循环,当/如果用户输入单词“STOP”时终止。
您还需要采用不同的方法来呈现用户输入。将输入限制为仅由字母 B、A 和 L 组成的单词有些限制。
你可以这样做:
matrix = {
"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": ["*****", " * ", " * ", " * ", "*****"],
" ": [" ", " ", " ", " ", " "],
".": [" ", " ", " ", " * ", " "],
",": [" ", " ", " * ", " * ", " "],
"!": [" * ", " * ", " * ", " ", " * "],
"?": [" *** ", " * ", " * ", " ", " * "],
"'": [" * ", " * ", " ", " ", " "],
'"': [" * * ", " * * ", " ", " ", " "],
"-": [" ", " ", " *** ", " ", " "],
"_": [" ", " ", " ", " *** ", " "],
"0": [" *** ", "* *", "* *", "* *", " *** "],
"1": [" * ", " ** ", " * ", " * ", " * "],
"2": [" *** ", " * ", " *** ", "* ", " *** "],
"3": [" *** ", " * ", " *** ", " * ", " *** "],
"4": ["* *", "* *", "*****", " *", " *"],
"5": ["*****", "* ", "**** ", " * ", "**** "],
"6": [" *** ", "* ", "**** ", "* * ", " *** "],
"7": ["*****", " * ", " * ", " * ", "* "],
"8": [" *** ", "* * ", " *** ", "* * ", " *** "],
"9": [" *** ", "* * ", " ****", " * ", " *** "],
"(": [" * ", " * ", "* ", " * ", " * "],
")": [" * ", " * ", " * ", " * ", " * "],
}
def pattern(word):
return [" ".join([matrix[c][i] for c in word]) for i in range(5)]
while (word := input("Enter a word or STOP to end the program: ").upper()) != "STOP":
if word and all(c in matrix for c in word):
print(*pattern(word), sep="\n")
else:
print("Invalid word")
只需使用一个循环并检查您的名称是否为 stop 来打破它
while True:
name = input("Enter the name:")
if name.lower() == "stop":
break
# your normal program execution
list2 = []
list3 = pattern()