388,677 views
11 votes
11 votes
I need help creating a symmetric histogram. The program should ask the user to enter the number of bars to print and the character to use to draw the bars. The count of characters in the bar needs to be displayed on the right-side of each bar. You must use a nested loop to display the output.

I need help creating a symmetric histogram. The program should ask the user to enter-example-1
User Canberk Sinangil
by
3.0k points

1 Answer

12 votes
12 votes

Answer:

The program in Python is as follows:

import math

bars = int(input("Bars: "))

symbol = input("Character: ")

current = 1

while current < bars * 2:

if current <= bars:

num = current

for i in range(num):

print(symbol,end="")

print(current)

else:

num = abs(bars * 2 - current);

for i in range(num):

print(symbol,end="")

print(i+1)

current+=1

Step-by-step explanation:

This imports the math module

import math

This gets input for the number of bars

bars = int(input("Bars: "))

This gets input for the number of characters

symbol = input("Character: ")

This sets the current element to 1

current = 1

The following iteration is repeated until printing ends

while current < bars * 2:

The following checks for printing in ascending order

if current <= bars:

num = current

The following loop prints the characters in ascending order

for i in range(num):

print(symbol,end="")

This prints the number of characters

print(current)

The following checks for printing in descending order

else:

Calculate the number of iteration left

num = abs(bars * 2 - current);

The following loop prints the characters in descending order

for i in range(num):

print(symbol,end="")

This prints the number of characters

print(i+1)

current+=1

User Mick MacCallum
by
2.5k points