This question already has an answer here:
Context: Assignment is to write a program that shall display a hollow diamond of stars with N rows and N columns, where N is a variable initialized with an odd positive number. You shall not use any string methods (such as .center()). An example of a diamond with N=7 is shown below:
*
* *
* *
* *
* *
* *
*
My Question: I have completed the diamond building problem (maybe not the best way but good enough to my satisfaction), but my issue is with the if statement at the beginning. I want to repeat the input phase when n is even or less than 3, however when testing the loop only works once and then exits and I cannot figure out why.
n = int(input("Please enter an odd number that is greater than or equal
than 3: "))
if (n < 3 or n % 2 == 0): #checks if even and less than 3
n = int(input("Invalid Entry.\nPlease enter an odd number that is greater than or equal than 3: "))
else: #if odd and greater than or equal to 3
e = (n - 1) // 2 #for spaces on top & bottom
sd = ( (n - 1) // 2) - 1 #for spaces on ends, will decrease
si = 1 #for spaces on ends, will increase
m = n - 2
b = 1 #middle spaces counter for top half
a = n - 4 #middle spaces counter for bottom half
#top one done out seperately becayse doesnt fit into loop
print((e * " ") + "*" + (e * " "))
while b <= m: #while middle spaces are less than or equal too input - 2
print(
( (sd) * " ") + #spaces left side
("*") +
(b * " ") + #spaces middle
("*") +
( (sd) * " ") #spaces right side
)
b += 2 #spaces middle increaser
sd -= 1 #spaces sides decreaser
while a >= 1: #while middle spaces are greater than or equal to 1
print(
( (si) * " ") + #spaces left side
("*") +
(a * " ") + #spaces middle
("*") +
( (si) * " ") #spaces right side
)
a -= 2 #spaces middle decreaser
si += 1 #spaces side increaser
print((e * " ") + "*" + (e * " "))
Aucun commentaire:
Enregistrer un commentaire