Input, Output, and Types

Part of: Programming Lab: Python for CSP

Talking to the User. Two functions handle the conversation. input() reads a line the user types, and print() shows a value on the screen. Whatever the user types comes back as text , also called a string . Even if they type 42, input() hands you the string "42", not the number 42. Types Matter. A type is the kind of value: int for whole numbers, float for decimals, str for text. You cannot add a number to a string directly, so you convert. int(...) turns text into a whole number and str(...) turns a number into text. Here int(age text) converts "20" into the number 20 so math works. Then str(next age) converts 21 back into text so it can join the sentence with +. Two Ways to Combine. With strings, + glues text together. With numbers, + adds. Mixing them without converting is a common error. When you want to print a number beside words, convert the number with str(...) first, or print them as separate arguments separated by a comma. What usually goes wrong. What you see What caused it How to fix it --- --- --- TypeError: can only concatenate str (not "int") to str A number was joined to text with + without being converted first Wrap the number in str(...), or pass the pieces to prin

Challenge: Double the Number