Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Formatting Help!
#1
Hi, I am trying to format the following code so that the stars are all aligned in a column, starting at the same spot, and the same with the months. The months are already aligned by default, but I can't get the stars to be aligned. I also need to align the total at the bottom. It should look like the following.
Example of Output:

Month Sales

January ****************************
February ******************************
March ************************
April **********
May ************
June **********
July *****
August ***
September ****
October **********
November *********************
December *************************
Total Sold for 20XX 182

Here is the code:
#Variable List
# months - the tuple for months
# sales - the corresponding sales for each month

print ("Welcome to the Artic Dog program")
#step 1 - this will create a tuple for the months and sales
months = ("January","February","March","April","May","June","July","August","September","October","November","December")
sales = (28,30,24,10,12,10,5,3,4,10,21,25)


# step 3 - this will print and format the months and bar graph
total = 0
count = 0
for item in months:
    print (item,"{:<30}".format(sales[count]*"*"))
    count = count + 1
for num in sales:
    total = num + total
print ("Total Sold for 20XX","{:<30}".format(total))
Reply
#2
So the most letters from a month is 9, so I just put extra spaces in between the month and stars based on how many letters were in that month. For example in June, there are an extra 5 spaces. You can take a look at the code below.
#Variable List
# months - the tuple for months
# sales - the corresponding sales for each month
 
print ("Welcome to the Artic Dog program")
#step 1 - this will create a tuple for the months and sales
months = ("January","February","March","April","May","June","July","August","September","October","November","December")
sales = (28,30,24,10,12,10,5,3,4,10,21,25)
 
# step 3 - this will print and format the months and bar graph
total = 0
count = 0
for item in months:
    print (item," "*(9-len(item)), "{:<30}".format(sales[count]*"*"))
    count = count + 1
for num in sales:
    total = num + total
print ("Total Sold for 20XX","{:<30}".format(total))
Reply
#3
Thanks!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020