TryBugHunt

TypeError: '>' not supported between instances of 'str' and 'int'

TypeError: '>' not supported between instances of 'str' and 'int' in Python means you compared text with a number. The value arrived as a string and was never converted.

PythonPattern: Type errors

How do I fix TypeError: '>' not supported between instances of 'str' and 'int'?

To fix TypeError: '>' not supported between instances of 'str' and 'int' in Python, locate the line causing the error and check the values right before it runs. Most occurrences happen because of input() always returns a string. Apply a defensive check or safe fallback, make sure collections or objects exist before access, and verify return values from upstream functions.

1. Inspect bounds & typesPrint or inspect collection sizes and verify variables are not empty or None/undefined.
2. Guard with safe accessUse optional chaining (?.), dictionary get(), or guard conditions before dereferencing.
3. Trace upstream originsFind where the value was instantiated or returned rather than only patching the symptom.

What does TypeError: '>' not supported between instances of 'str' and 'int' mean?

Python 3 refuses to order unrelated types rather than guessing. This is deliberate — Python 2 allowed it and produced meaningless results. Anything from input(), a form, a CSV, JSON or a query string arrives as text.

Why does TypeError: '>' not supported between instances of 'str' and 'int' happen?

1. input() always returns a string

Even when the user types digits, you get "42", not 42.

Breaks

age = input("Age: ")
if age > 18:
    print("adult")

Works

age = int(input("Age: "))
if age > 18:
    print("adult")

2. Values from a file or API

CSV columns and query parameters are text until you convert them.

Breaks

if row["score"] > 50:

Works

if int(row["score"]) > 50:

How to find it in your own code

Convert once, at the boundary where data enters your program, rather than scattering int() through your logic. When a value surprises you, print its type before its value.

Try it: fix this bug

easy

sort_users_by_priority raises TypeError: '<' not supported between instances of 'NoneType' and 'int'.

def sort_users_by_priority(users):
    return sorted(users, key=lambda u: u["priority"])
Open with hintsRuns in your browser. No account needed.

More challenges with this error

Each of these challenges runs into this error or pattern. Find the bug and fix it in your browser — no install, no account needed.

Still not sure why yours breaks?

Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.

Related error guides

View all type errors →