1

I made a program that would some up a grade average. Everything seems fine, the only problem, when I run the program at the end it prints out like this:

Jack
5
10
6
7.0
Stacy
10
8
9
9.0
Jack
5
10
6
7.0
Stacy
10
8
9
9.0
Jack
5
10
6
7.0
Stacy
10
8
9
9.0

How can I make the program print out, for example:

Name Grade1 Grade2 Grade3 (etc) Average
Name2 Grade1 Grade2 Grade3 (etc) Average2

etc

separately for every student's name used?

grades = []       
names = []         
ch = 1               
while (ch!=0):                         
    name = input('Enter a name: ')         
    if (name.isalpha()):           
        grade = int(input('Enter a grade: '))
        if(grade <= 10 and grade > 0):     
            names.append(name)         
            grades.append(grade)       
        else:
            while (grade <= 0) or (grade > 10):
                print ('The grade you entered is not correct, try again!')
                grade = int (input('Enter a grade: '))
            names.append(name)
            grades.append(grade)
    elif(name == '0'):
        print ('A zero was entered, end of program.')
        ch = 0   
    else:
        print ('The name was entered wrong, try again!')
#problem probably starts from here
howmuch = len(grades)                   
result = []                               
for k in range(0, howmuch):
    nreal = names[k]                             
    for i in range(0, howmuch):           
        if (nreal == names[i]):         
            result.append(grades[i])
    greal = len(result)
    sumup = 0
    print (nreal)
    howmuch = len(names)
    for z in range(0,greal):
        a = result[z]
        b= int(a)
        print (b)
        sumup = sumup + b
    avg = sumup /(z+1)
    print (avg)
    result = []
acurate
  • 105
  • 1
  • 1
  • 8
  • What exactly is your desired output? – miradulo Mar 23 '15 at 00:03
  • So that it would be like: Name Grade Average/ Name2 Grade2 Average2 So it would be in one line for each student. – acurate Mar 23 '15 at 00:04
  • 1
    Use print(variable, end = " ") (Notice the comma after the variable) Check [this](http://stackoverflow.com/questions/18908897/whats-ending-comma-in-print-function-for) link – koukouviou Mar 23 '15 at 00:13

3 Answers3

0

Add a comma at the end of the print statements where you do not want a new line.

print nreal,
...
    print b,
    ...
print avg
Julien Spronck
  • 15,069
  • 4
  • 47
  • 55
  • right ... i was confused by the space between print and the parentheses ... which would be correct in python 2 but i see there is also an `input` – Julien Spronck Mar 23 '15 at 00:24
0

by default python adds a \n to the end of every print call. To fix that add a second parameter to print. print(string, end="")

JGerulskis
  • 800
  • 2
  • 10
  • 24
0

In Python 3, which is what you seem to be using, you can print "horizontally" by setting the end argument of the print function, which is "\n" (a newline) by default, like this:

...
print (nreal, end = " ")
...
   print (b, end = " ")

Leave your last print statement as is: print (avg) since you want it to print a newline for printing the next student's information.

Community
  • 1
  • 1
koukouviou
  • 820
  • 13
  • 23