Rocket writes a program that asks how many crates arrived, then doubles it for the order form. The user types 4. The screen proudly shows 44.
"Forty-four?" Rocket says. "Four plus four is eight!" Raven laughs kindly. "What do you notice? Where have we seen 3 and 4 make 34 before?"
Rocket remembers week one. "Strings!" Nova glows. "Great discovery! Would you like a hint? Python has a command that turns digits into a real number."
input always gives back a string, even when the user types digits.
Plus joins strings, so "4" + "4" makes "44".
int turns a string of digits into a number. Then plus adds.
crates = input("Crates in? ")
print(crates + crates) | The screen shows | The user types |
|---|---|
| Crates in? | 4 |
| 44 |
crates = int(input("Crates in? "))
print(crates + crates) | The screen shows | The user types |
|---|---|
| Crates in? | 4 |
| 8 |
int(input("Crates in? ")) does two jobs. input gets the string "4". Then int turns it into the number 4.
| Expression | Is the result a number? |
|---|---|
| int("12") | ? |
| input("Age of the ship? ") | ? |
| "5" + "5" | ? |
| int("3") + 2 | ? |
a = input("First? ")
b = input("Second? ")
print(a + b)
print(int(a) + int(b)) | The screen shows | The user types |
|---|---|
| First? | 3 |
| Second? | 9 |
| 39 | |
| 12 |
| Statement | True or false? |
|---|---|
| "6" + "1" makes "61". | ? |
| int("6") + 1 makes 7. | ? |
| int("six") makes 6. | ? |
| input turns digits into numbers by itself. | ? |
Sharp thinking! Tomorrow you will use numbers from the user to work things out.