Input and Casting
Quiz: counts toward finishing the lesson. Check: just for practice.
Input function
The input() function reads text typed by the user. You can use it in two ways.
For example, to ask for the user's name, you can do this:
Or you can do this:
With the first way, the user types on the line after the prompt. With the second way, the user types on the same line as the prompt.
User input is always a string. To get a number, you need to cast the string to an int or a float. Casting means converting a value from one type to another.
Each of the first two lines above combines two functions. First input() gets a
string from the user. Next int() or float() casts that string to a number.
That number is then assigned to the variable. We can also call those functions
on separate lines:
If the user enters text that cannot be cast to the requested type, Python reports an error. For example, what happens if you enter "hi" for the height in meters in the earlier example? Try it.
Here is an example that asks the user for two numbers and prints out their sum:
You can also cast an int to a float:
Or a float to an int, which truncates everything after the decimal point:
Check your understanding. Predict the result. One value is cast to a number and one stays a string.
typed = "5"
x = int(typed) + 1
y = typed + "1"
Predict what each variable holds after the program finishes. The program is not run for you. Change the values below to your predictions, and write strings with quotes:
final_xis the value ofxat the endfinal_yis the value ofyat the end
Hint 1
int() makes a number, so + adds. A string stays a string, so + joins.
You can also cast values to strings with str().
Check your understanding. Predict the result. Check what int() does to a decimal, and what str() gives back.
n = 42
x = int(9.99) + 1
y = str(n) + "0"
Predict what each variable holds after the program finishes. The program is not run for you. Change the values below to your predictions, and write strings with quotes:
final_xis the value ofxat the endfinal_yis the value ofyat the end
Hint 1
int() truncates. Adding a string to a string joins them.
Quizzes
The quizzes below are written as functions you can test without typing input.
In a real program you would read these values with input(), but a function
that takes its values as parameters is easier to test and reuse.
- Write a function named
greetthat takes anameparameter and prints out"Hello"followed by the name. For example,greet("Sam")should printHello Sam.
- Write a function named
multiplythat takes two numbers and prints out"The result is"followed by the result of multiplying those numbers. For example,multiply(3, 4)should printThe result is 12.
Write a function named
divide_reportthat takes two integers, adividendand adivisor, and prints a report in this exact form:<divisor> goes into <dividend> <i> times with <r> as the remainder.
where <i> is the whole number of times the divisor goes into the dividend and
<r> is the remainder. For example, divide_report(17, 5) should print:
`5 goes into 17 3 times with 2 as the remainder.`
Hint 1
Use floor division // to get how many whole times the divisor fits, and the
mod operator % to get the remainder.
Projects
Practice what you learned in your own editor. Each project has its own page.