I need the below specific working input output letter to star pattern code to loop until I type ‘stop’. This is specifically related to grids, iterations and conditionals in relation to Letter to Star Pattern.
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 ()
I have the entire alphabet to add to this code eventually but can’t figure out how to make the below loop and input codes to work with the above code. The below code is out of one of my old programs and the above program is newly written by me but won’t work with the below code.
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
and…
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()
>Solution :
First, pattern should take the input word as argument, and should declare list2 as a local name:
def pattern(name): # Parameter
list2 = [] # Moved here as a local name
Then, in the else block of your main, call pattern like this:
else:
list3 = pattern(word)
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 ()
This similar code should be removed from your first code snippet.
So in total:
def pattern(name): # Parameter
list2 = [] # Moved here as a local name
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
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:
list3 = pattern(word)
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 ()
if __name__ == '__main__':
main()