1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Python: print problem

  1. Mar 22, 2015 #1
    1. The problem statement, all variables and given/known data
    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 (Text):

    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?


    3. The attempt at a solution
    Code (Text):

    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 = []
     
     
  2. jcsd
  3. Mar 22, 2015 #2

    jedishrfu

    Staff: Mentor

    Try replacing your prints with string appends and the last print statement to print the appended string:

    pstring = ""+real
    ...
    pstring = pstring+" "+b
    ...
    print pstring
     
  4. Mar 23, 2015 #3
    By default python outputs a space and newline when you use print
    so if you did:
    Code (Python):

    print "Hello"
    print "bob"
     
    you'll get:
    Hello
    bob

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

    Code (Python):

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

    or you can use jedishrfu's solution as well :)
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted