Formatting Help! - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Formatting Help! (/thread-25317.html) |
Formatting Help! - DudleyDiccle - Mar-26-2020 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)) RE: Formatting Help! - SheeppOSU - Mar-26-2020 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)) RE: Formatting Help! - DudleyDiccle - Mar-26-2020 Thanks! |