Fix Python Print Problem: Get Student Grade Averages

  • Thread starter acurate
  • Start date
  • Tags
    Python
In summary, the conversation discusses a program that calculates a grade average and prints out the results for each student. However, the program currently prints out the results for all students together. The solution involves using string appends and suppressing the newline in the print function to create separate outputs for each student.
  • #1
acurate
17
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
  • #2
Try replacing your prints with string appends and the last print statement to print the appended string:

pstring = ""+real
...
pstring = pstring+" "+b
...
print pstring
 
  • #3
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

Related to Fix Python Print Problem: Get Student Grade Averages

1. What is the "Fix Python Print Problem"?

The "Fix Python Print Problem" is a common coding issue where a Python program is unable to accurately print the desired output. In this specific case, the program is being used to calculate and print student grade averages.

2. How can I fix the Python print problem?

To fix the Python print problem, you will need to carefully review the code and identify any errors or mistakes. Some common causes of this problem may include incorrect syntax, missing or incorrect variables, or improper use of functions. Once the issue is identified, you can make the necessary changes to the code to ensure that the desired output is printed correctly.

3. Why is it important to fix the Python print problem?

Fixing the Python print problem is important because it ensures that the program is functioning as intended and producing accurate results. If the issue is not addressed, it could lead to incorrect data being printed, which could have consequences for the users relying on the program's output.

4. Can I prevent the Python print problem from occurring in the future?

While it is impossible to prevent all coding issues from occurring, there are some steps you can take to minimize the chances of encountering the Python print problem in the future. These include writing clean and organized code, testing the program thoroughly before use, and regularly reviewing and debugging the code.

5. Are there any resources available to help with fixing the Python print problem?

Yes, there are many resources available to help with fixing the Python print problem. These include online forums and communities where you can ask for assistance from other programmers, as well as tutorials and guides that provide step-by-step instructions on how to troubleshoot and fix common coding issues.

Similar threads

  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
Replies
8
Views
925
Replies
6
Views
2K
Back
Top