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 the number of bars to print and the character to use to draw the bars The co class=

Respuesta :

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

   

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