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. Key takeaways: - input() always returns a string. - int(...) converts text to a whole number; str(...) converts a number to text. - + joins strings but adds numbers. - Convert before mixing text and numbers.

Challenge: Double the Number