0

While writing some code for a class, it gave this error and I don't know how to fix it as I personally do not see any issues with what it is addressing

traceback (most recent call last):
  File "C:\Users\Daniel Benotti\Desktop\School\Benotti_Daniel student Ed loop with functions.py", line 50, in 
    main()
  File "C:\Users\Daniel Benotti\Desktop\School\Benotti_Daniel student Ed loop with functions.py", line 9, in main
    TotCurr, TotCe = loop(TotCurr, TotCe, again)
  File "C:\Users\Daniel Benotti\Desktop\School\Benotti_Daniel student Ed loop with functions.py", line 21, in loop
    TotCurr, TotCe, studType = calc(studNum, TotCurr, TotCe)
  File "C:\Users\Daniel Benotti\Desktop\School\Benotti_Daniel student Ed loop with functions.py", line 34, in calc
    if studNum > STUD_NUMBER:
TypeError: '>' not supported between instances of 'str' and 'int'`enter code here`

this is the code that follows

STUD_NUMBER = 2500

def main():
    TotCurr, TotCe, again = init()
    TotCurr, TotCe = loop(TotCurr, TotCe, again)
    dispTot(TotCurr, TotCe)

def init():
    TotCurr = 0
    TotCe = 0
    again = "Y"
    return TotCurr, TotCe, again

def loop(TotCurr, TotCe, again):
    while again.upper() == "Y":
        firstName, lastName, studNum = getInput()
        TotCurr, TotCe, studType = calc(studNum, TotCurr, TotCe)
        dispDetail(studType)
        again = input("Do you want to add another student record? (Y/N): ")
        print()
    return TotCurr, TotCe

def getInput():
    firstName = input("please enter students first name: ")
    lastName = input("please enter students last name: ")
    studNum = input("please enter the student ID number: ")
    return firstName, lastName, studNum

def calc(studNum, TotCurr, TotCe):
    if studNum > STUD_NUMBER:
        studType = "Curriculum"
        TotCurr += 1
    else:
        studType = "Continuing Education"
        TotCe +=1
    return TotCurr, TotCe, studType

    def dispdetail(studType):
        print("This student is currently in the", studType, 'course')
        print()

    def dispTot(TotCurr, TotCe):
        print("Total students in Cuuriculum course", TotCurr)
        print("Total students in Continuing education course", TotCe)

main()
enter code here
Donni Benotti
  • 19
  • 1
  • 5

1 Answers1

1

It looks like you're comparing a String and an Integer

do this:

if int(studNum) > STUD_NUMBER:

This is called casting. You will be converting a string to an int by using this function: int()

How to compare string and integer in python?

  • I fixed that and now its telling me that my dispDetail isn't defined which it is – Donni Benotti Oct 31 '18 at 22:08
  • 1
    You're calling it before it being defined. Python is an interpreted language. Put that in mind when declaring functions after calling them. Further reading: https://www.quora.com/How-does-the-Python-interpreter-handle-function-calls-in-lines-before-the-actual-function-definition-without-using-forward-declarations-like-in-C++ – christopher de jesus Oct 31 '18 at 22:09
  • Thats the strange thing though, as I have another set of the code that has that same setup, but it works. – Donni Benotti Oct 31 '18 at 22:30
  • 1
    The functions defined inside the function are also defined after a return. That code is unreachable. Put it outside. – christopher de jesus Oct 31 '18 at 22:32
  • I see it now. It was an indentation error. those always get me – Donni Benotti Oct 31 '18 at 23:09
  • This pyhton class has been fun so far, but im not so quick on the uptake and some small things I miss really easily at times or I just dont get at all, so I appreciate the help – Donni Benotti Oct 31 '18 at 23:12