Fix Python Print Problem: Get Student Grade Averages

  • Context: Python 
  • Thread starter Thread starter acurate
  • Start date Start date
  • Tags Tags
    Python
Click For Summary
SUMMARY

The forum discussion addresses a Python programming issue where a student’s grade averages are printed multiple times instead of once per student. The user’s code collects names and grades but fails to format the output correctly. The solution involves modifying the print statements to concatenate strings and control line breaks, ensuring each student's name and average are displayed separately. Key suggestions include using string appends and adjusting print functions to suppress automatic newlines.

PREREQUISITES
  • Basic understanding of Python programming
  • Familiarity with lists and loops in Python
  • Knowledge of input handling and data validation in Python
  • Experience with string manipulation in Python
NEXT STEPS
  • Learn about Python string formatting techniques
  • Explore Python list comprehensions for more efficient data handling
  • Investigate Python functions for modular programming
  • Study error handling in Python to improve user input validation
USEFUL FOR

Students learning Python, educators teaching programming concepts, and developers seeking to improve their output formatting skills in Python applications.

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   Reactions: jedishrfu

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
3K
  • · Replies 17 ·
Replies
17
Views
3K
Replies
55
Views
7K
  • · Replies 17 ·
Replies
17
Views
4K