These questions follow Data in Programs. Every program here talks to a person — which means a person can always type the unexpected.
Questions
- Predict the output of each line — they are not the same.
print("Age:", 15) print("Age:" + "15") - Predict the output when the user types
Samat the prompt.name = input("Who is this? ") print("Hi, " + name + "!") print("Bye, " + name + ".") - Find the bug. Why does this crash, and what one change fixes it?
age = input("How old are you? ") print("Next year you will be", age + 1) - In
n = int(input("Number: ")), describe what happens when the user types7— and then what happens when they typeseven. - Write a short program: ask for two numbers, print their sum. Test
it with
3and4— if it prints34, you have found question 3. - Challenge. Ask for a word and a number, then print the word
that many times on one line.
catand3should givecatcatcat.
Answers
Answer 1
Age: 15thenAge:15. A comma in+glues two strings with nothing added. Different tools.
Answer 2
Two lines:
Hi, Sam!thenBye, Sam.— read once, reused twice.
Answer 3
ageis a string —input()always returns text — soage + 1adds text to a number: aTypeError. Wrap the input inint().
Answer 4
7becomes the number7and all is well.sevendefeatsint()completely, and the program stops with aValueError.
Answer 5
first = int(input("First number: ")) second = int(input("Second number: ")) print("Sum:", first + second)
Answer 6
word = input("Word: "),times = int(input("How many? ")), thenprint(word * times)— multiplying a string repeats it.
Curriculum connection
C1.3
identify various types of data and explain how they are used within programs
Link to original
C2.1
use variables, constants, expressions, and assignment statements to store and manipulate numbers and text in a program
Link to original
C2.2
write programs that use and generate data involving various sources and formats
Link to original