215k views
0 votes
Write a program to display the following diamond-shape like pattern based on the height given by the user. Assume that the user will always enter a positive odd number.

User Mosab Sasi
by
5.8k points

1 Answer

1 vote

Answer:

try:

size = int(input("Enter a number: "))

except ValueError:

print("must be a number")

if size==0:

print("number must be greater than zero.")

elif size%2==0:

print("number must be odd.")

else:

obj = '#'

mid_size = round(size/2)

s = size - mid_size

count = 2

counts = 1

for i in range(size+1):

if i<=mid_size:

print(" " *(size - i) + (obj+" ") * i)

elif i>mid_size:

print(" " * (s + counts) + (obj + " ") * (i - count))

count += 2

counts += 1

Step-by-step explanation:

The python source code uses a try and except statement to check for value error and print a diamond shape of the object "#" determined by the input number.

User Tonny Tc
by
4.9k points