Question

I'm trying to make a program that calculates how much it costs to miss a class. I take in variables tuition, number of courses, and number of weeks in a semester; it then plots the results (using python 2.7). Here's the code I've been working on:

import matplotlib.pyplot as plot

def calculations(t, c, w, wk):

    two_week = (((t/c)/w)/2)*wk
    three_week = (((t/c)/w)/3)*wk
    return two_week, three_week

def main():
    tuition = float(raw_input('Tuition cost (not including fees): '))
    courses = int(raw_input('Number of courses: '))
    weeks = int(raw_input('Number of weeks in the semester: '))
    x_axis = range(0,10)
    y_axis = []
    y_axis2 = []
    for week in x_axis:
        cost_two, cost_three = calculations(tuition, courses, weeks, week)
        y_axis += [cost_two]
        y_axis2 += [cost_three]

    plot.plot(x_axis, y_axis ,marker='o', label='course meets 2x a week', color='b')
    plot.plot(x_axis, y_axis2,marker='o', label='course meets 3x a week', color='g')
    plot.xlabel('number of missed classes')
    plot.ylabel('cost ($)')
    plot.title('Tuition: %.2f Courses: %d Weeks in a semester: %d\n Created by <MyName>') %(tuition, courses, weeks)
    plot.legend()
    plot.xticks(x_axis)
    plot.xlim(0,x_axis[-1]+1)
    plot.yticks(y_axis)
    plot.ylim(0, y_axis[0]+1)
    plot.grid()
    plot.show()
    plot.savefig('missing-class-cost.pdf')

main()

However, whenever I run my program, I receive the following error:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 36,     in <module>
  File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 26, in main
TypeError: unsupported operand type(s) for %: 'Text' and 'tuple'

Line 26 is in reference to this line of code:

plot.title('Tuition: %.2f Courses: %d Weeks in a semester: %d\n Created by <MyName>') %(tuition, courses, weeks)

I'm assuming it's due to some math, but I didn't use a tuple in the whole program, so I'm kind of at a loss.

Any help is appreciated, thanks.

Was it helpful?

Solution

Your parentheses are in the wrong place. You probably want

plot.title('Tuition: [...]' %(tuition, courses, weeks))

instead. Right now, you're doing

plot.title('Tuition: [...]') %(tuition, courses, weeks)

so you're calling plot.title, which is returning a Text object, and then you're trying to call % on that, which gives rise to the error message:

TypeError: unsupported operand type(s) for %: 'Text' and 'tuple'

which hopefully should make more sense now. Even though you say "I didn't use a tuple in the whole program", that's exactly what (tuition, courses, weeks) is.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top