Is this a clear way to explain Python’s input() to beginners?

Hi everyone! I’m a Computer Science student preparing a short Python tutorial for beginners. I’d appreciate feedback on this explanation.

Python’s input() returns a string, even when someone types a number. For example:

age = input("Enter your age: ")
print(age + 1)

This raises a TypeError because Python cannot add a string and an integer.

If we expect a whole number, we can convert the input first:

age = int(input("Enter your age: "))
print("Next year you will be", age + 1)

Entering 20 now gives 21. However, entering something like "twenty" raises a ValueError.

For an introductory tutorial, would you explain int() first and introduce try/except afterward, or show both together? I want to keep the explanation simple without overlooking invalid input.

I guess you should first introduce the concept of variables and different data types, which includes how strings, integers, floats, lists etc interact together, before try/except because these are more of a debugging problem, and they are not the primary concern when first learning about variables. If you want to learn Python theory in a general way, and not for example giving info about a specific project that will need specific skills including working with debugging and the int() method, I think it will be confusing to mix both steps together. Maybe try (if you’re not doing it already) to take some inspiration from the Python curriculum? I am not an expert, and I think the people who designed it are more qualified than me, so it will be a better source.

Hope this helped you, happy coding

Welcome to the forum @CodingWithLin

Explaining a type error is a good way to introduce students to tracebacks.

Then posing a question, like how “How can I combine user input with a number?”, with your initial example is a good way for students to learn debugging.

Next introduce them to the typefunction so the student can inspect the variable. At a later stage teach them about the isinstance function.

This way you are not jumping to the answer, and the student gets to understand some basic debugging techniques.

Then progress to the int function.

For beginners maybe leave try / except / finally until later. Get them to understand basic data structures like lists, dictionaries, sets, then move onto functions.

Happy coding

Yo that’s cool man… Thanks for the advice by the way…I’ll use them in the future.