Python Fix Python Print Problem: Get Student Grade Averages

  • Thread starter Thread starter acurate
  • Start date Start date
  • Tags Tags
    Python
Click For Summary
The discussion focuses on a Python program designed to calculate and print student grade averages, but it currently outputs repeated names and grades in an unformatted manner. The user seeks a solution to display each student's name alongside their average grade in a clear format. Suggestions include replacing print statements with string concatenation to build a formatted output. Additionally, using a comma in print statements can suppress newlines, allowing for a more compact display. The conversation emphasizes the need for proper formatting to enhance readability in the output.
acurate
Messages
17
Reaction score
1

Homework Statement


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:
Code:
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 Grade Average
Name Grade Average

separately for every student's name used?


The Attempt at a Solution


Code:
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)
    how much = 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 = []
 
Technology news on Phys.org
Try replacing your prints with string appends and the last print statement to print the appended string:

pstring = ""+real
...
pstring = pstring+" "+b
...
print pstring
 
By default python outputs a space and newline when you use print
so if you did:
Python:
print "Hello"
print "bob"
you'll get:
Hello
bob

adding a , after the print will suppress the newline eg:

Python:
print "Hello",
print "bob"
results in:
Hello bob

or you can use jedishrfu's solution as well :)
 
  • Like
Likes jedishrfu
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
Replies
55
Views
6K
  • · Replies 17 ·
Replies
17
Views
3K