Practice question · Put in order
Order the steps a program takes to read a number from the user and add ten to it.
- Read the input, which arrives as a string
- Convert the string to an integer with int(...)
- Prompt the user to type a number
- Print the result
- Add ten to the integer
Hints
- The conversion must happen before any arithmetic, because the input is text.
- Adding before converting would try to add to a string.
Show the answer
- Prompt the user to type a number
- Read the input, which arrives as a string
- Convert the string to an integer with int(...)
- Add ten to the integer
- Print the result
Why
Prompt, read text, convert to a number, then compute, then print. Doing the arithmetic before the conversion is the classic failure, it either joins strings or raises an error, because the input is text until int(...) turns it into a number.
Practise Input, Output, and File Thinking
The app has 5 more questions on this lesson, and keeps your place in the course. Computer Science I is free to start.
More questions on Input, Output, and File Thinking
- The open-process-close discipline requires closing a file even when processing fails partway through. Why…
- Match each I/O idea to what it means.
- A program writes to a file, crashes, and the file turns out empty, although the writes clearly executed.…
- Sort each action by whether it is input or output.
- Select every statement that is true of the stream model and file handling.