0

Here is a simplified snapshot of my code:

def getExamPoints(examPoints):
    totalexamPoints = 0 
    return totalexamPoints

def getHomeworkPoints(hwPoints):
    totalhwPoints = 0
    return totalhwPoints

def getProjectPoints(projectPoints):
    totalprojectPoints = 0
    return avgtotalprojectPoints

def computeGrade(computeGrade):
    computeGrade = 1
    return computeGrade

def main ( ):

    gradeReport = "\n\nStudent\t Score\tGrade\n=====================\n"

    studentName = input ("Enter the next student's name, or 'quit' when done: ")

    while studentName != "quit":

        examPoints = getExamPoints(studentName)
        hwPoints = getHomeworkPoints(studentName)
        projectPoints = getProjectPoints(studentName)

        studentScore = examPoints + hwPoints + projectPoints

        studentGrade = computeGrade (studentScore)

        gradeReport = gradeReport + "\n" + studentName + "\t%6.1f"%studentScore + "\t" + studentGrade** 

main ( ) # Start program

Getting an error on gradeReport assignment on last line which says "can't convert int object to str implicitly". Why is it so?

sashkello
  • 17,306
  • 24
  • 81
  • 109
  • Don't use `+` to create strings, use the [format string syntax](http://docs.python.org/2/library/string.html#format-string-syntax). – Burhan Khalid Feb 26 '14 at 05:17
  • Question is not clear. What exactly is the problem? – thefourtheye Feb 26 '14 at 05:17
  • Please only put the part of the program that is relevant to the question; and not everything. – Burhan Khalid Feb 26 '14 at 05:21
  • 1
    @BurhanKhalid Mate, don't start edit wars here. The question is not clear without the knowledge of types of variables present. If you want to do a proper job, then remove information which is truly irrelevant. SO is full of questions where people put in one line from their code with no context and it is bad, as well as huge code dumps. – sashkello Feb 26 '14 at 05:22
  • I didn't mean to start an edit war, but its quite clear from that line and the exception raised what is the problem; the other lines of code did not add towards the problem. – Burhan Khalid Feb 26 '14 at 05:40

1 Answers1

2

This is the line in question:

gradeReport = gradeReport + "\n" + studentName + "\t%6.1f"%studentScore+"\t"+studentGrad

You should use string formatting instead of simply adding strings and ints together (which is an error). One way would be:

gradeReport = '%s\n%s\t%6.1f\t%s' % (gradeReport, studentName, studentScore, studentGrade

There are other ways, but since you were already using % formatting, I've continued that here


In Python, "5" + 5 is an error, because it's ambiguous. Should it return "55"? 10? Rather than make assumptions and be wrong half the time, Python requires you to be explicit about it.

mhlester
  • 22,781
  • 10
  • 52
  • 75