Bottom Page

Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Align columns
#1
Hi all,

Sorry for what is probably a really stupid question but, I started a data analytics course this week, having never done any coding before and unfortunately the tutor wasn't great for absolute beginners.

We have been set a lot of homework, most of which I have managed to workout but, I am stuck with just one thing.

The print I do at the end, how do I get it all to align in 3 perfect columns and how do I add titles at of the columns? For the former I have tried just about everything and I think I am making myself more and more confused.

Thanks for your help.

gm = 0
gm1 = 0
gm2 = 0
gm3 = 0
gm4 = 0
gm5 = 0
gm6 = 0
gm7 = 0
gm8 = 0
gm9 = 0

for x in data:
    if x in range(0,10):
        gm += 1
    elif x in range(10,20):
        gm1 += 1
    elif x in range(20,30):
        gm2 += 1
    elif x in range(30,40):
        gm3 += 1
    elif x in range(40,50):
        gm4 += 1
    elif x in range(50,60):
        gm5 += 1
    elif x in range(60,70):
        gm6 += 1
    elif x in range(70,80):
        gm7 += 1
    elif x in range(80,90):
        gm8 += 1
    elif x in range(90,100):
        gm9 += 1
            

grademark = [gm,gm1,gm2,gm3,gm4,gm5,gm6,gm7,gm8,gm9]
grades = ["0 - 10",
             "10 - 20",	 
             "20 - 30 ",
             "30 -40",	 
             "40-50",
             "50 - 60",
             "60 - 70",	 
             "70 - 80 ",
             "80 - 90",	 
             "90 - 100"]

for i in range(len(grades)):
    print("{:}{:6}{:6}".format(grades[i], grademark[i],'*' * grademark[i]))
Moderator Note: Closing python tag needs '/' preceeding 'python'
Quote
#2
Please explain the assignment because your code looks to be doing an awful lot to accomplish something very simple.
your print statement is missing width for first item '{:}' so the : is not needed, and don't know what the * is doing by itself,
to multiply, you needs multiplicand and multiplier.
Quote
#3
I think the main thing to do would be to make sure all the strings in the grades list are the same width. I would pad them with spaces and make sure the spaces around the dashes are consistent. Then I would spaces between the columns in the string you are formatting on line 48.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#4
Hi Larz60+

Basically we have a sample of grades (detailed below) and we have to calculate how many times a grade falls into in range i.e 0-10, 10-20 etc etc. For each occurrence within that range we have to detail a * - i.e if 7 grades fall between 0-10 then there would be 7 *.

[python]
data=[90,30,13,67,85,87,50,45,51,72,64,69,59,17,22,23,44,25,16,67,85,87,50,45,51,72,59,14 ,50,55,32,23,24,25,37,28,39,30,33,35,40,34,41,43,94,95,98,99,44,45,47,48,49,53,61,63,69,7 5,77,60,83]
[python]

The end result being that it prints the below all in 3 nicely aligned columns, one column for grade, one for mark and one for the *'s

Mark Grade
0-10 0
10-20 4 ****
20-30 7 *******
30-40 8 ********

I imagine this code could be shortened considerably but, we are only using what we have been vaguely shown thus far. That being said, if anybody would like to comment with improvements then I would welcome any input for future progression and learning outside of course content.
Quote
#5
the data you show is a list, so I guess it's OK to use them
have you learned about dictionaries or enumeration yet?

Here's how you can simplify:
data=[
    90,30,13,67,85,87,50,45,51,72,64,69,59,17,22,23,44,
    25,16,67,85,87,50,45,51,72,59,14 ,50,55,32,23,24,25,
    37,28,39,30,33,35,40,34,41,43,94,95,98,99,44,45,47,
    48,49,53,61,63,69,75,77,60,83
]

grade_groups =  [0] * 10

for n, group in enumerate(range(11, 101, 10)):
    for grade in data:
        if grade < group and grade > (group - 10):
            grade_groups[n] += 1

print('{:12s}{:10s}{:10s}'.format('Mark', 'Grade', 'Stars'))
for n, group in enumerate(range(11, 101, 10)):
    print('{:3s}- {:7s}{:10s}{:14s}'.format(str(group-11), str(group-1), 
        str(grade_groups[n]), '*' * grade_groups[n]))
results:
Output:
Mark Grade Stars 0 - 10 0 10 - 20 4 **** 20 - 30 9 ********* 30 - 40 7 ******* 40 - 50 12 ************ 50 - 60 5 ***** 60 - 70 6 ****** 70 - 80 4 **** 80 - 90 6 ******
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Pass 2 columns via loc to lambda in pandas fad3r 1 1,748 Feb-22-2018, 09:57 PM
Last Post: glidecode
  Obtaining values from columns oncebuddy 1 470 Jan-22-2018, 06:42 AM
Last Post: j.crater
  Displaying a long list with Rows and Columns ngr33n 5 1,631 Sep-21-2017, 10:17 PM
Last Post: ngr33n
  pandas dataframe substracting columns: key error metalray 2 2,282 Feb-24-2017, 07:59 AM
Last Post: metalray

Forum Jump:


Users browsing this thread: 1 Guest(s)