Testing a program that uses input()

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Testing a program that uses input()

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Your test plan lists inputs 4 then 6 for a program that calls input() twice. What should you do when you run it?
A Type 6 first, because the last input is the most important
B Type 4 at the first prompt, then 6 at the second prompt
2. A test case says: input 5, expected output 10. Which step is part of testing a program that uses input()?
A Run it, type 5 when asked, then compare the printed output with 10
B Run it, type any number you like, then decide if the output looks right
3. You test the same program twice. Which is the better way to repeat the test?
A Type exactly the same planned inputs on each run
B Type different inputs each run so it feels fresh
4. After age = input("Age: "), what type of value is stored in age when you type 15?
A Text (a string) "15"
B A whole number 15
5. The code is x = input("Number: ") then print(x + x). You type 4. What is printed?
A 8
B 44

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1age = input("Age: ") AShows the actual output, which the tester compares with the expected result written in the test plan.
2print("Total:", total) Bnum = int(input("Number: "))
3When testing, type the planned inputs in the order the program asks for them, and use the same inputs on every run. CThe tester typed the inputs in a different order from the plan, so the values went into the wrong variables and the test is not repeatable.
4input() returns text, so a typed 7 arrives as the string "7" and must be converted before doing maths. Dinput() returns text, so "3" + "4" joins the strings and prints 34, not 7. The actual output will not match the expected result.
5a = input("A: ") b = input("B: ") print(a + b) # Test: type 3 and 4, expect 7 ETest 1: type 4, then 5, then check the output is 20
6# Test plan: type 10, then 2 name = input("Name: ") age = input("Age: ") # Tester types 2, then 10 FPauses the program so the tester can type a test value. Whatever is typed is stored in age as text.

Definitions

input()

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
age = _____("Enter your age: ") print(age)
2.
n = ___(input("Number: ")) print(n * 2)
3.
a = input("First: ") b = input("Second: ") print(___(a) + int(b))
4.
x = input("Enter a number: ") print(____(x))
5.
actual = "Hi " + input("Name: ") print(actual __ "Hi Sam")

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A a = input("First: ")
B b = input("Second: ")
C print(a + b)
Question 2
A name = input("Name: ")
B score = int(input("Score: "))
C if score >= 50:
D print(name, passed)
Question 3
A print("Enter a number")
B n = int(input)
C print(n * 2)
Question 4
A p = float(input("Price: "))
B q = int(input("Qty: "))
C total = p + q
D print("Total:", total)
Question 5
A guess = int(input("Guess: "))
B secret = 7
C if guess = secret:
D print("Correct")

Answer Key

Testing a program that uses input()
Section A: Quick Questions
Q1
B — Type 4 at the first prompt, then 6 at the second prompt
Q2
A — Run it, type 5 when asked, then compare the printed output with 10
Q3
A — Type exactly the same planned inputs on each run
Q4
A — Text (a string) "15"
Q5
B — 44
Section B: Match the Pairs
Q1
age = input("Age: ") → Pauses the program so the tester can type a test value. Whatever is typed is stored in age as text.
Q2
print("Total:", total) → Shows the actual output, which the tester compares with the expected result written in the test plan.
Q3
When testing, type the planned inputs in the order the program asks for them, and use the same inputs on every run. → Test 1: type 4, then 5, then check the output is 20
Q4
input() returns text, so a typed 7 arrives as the string "7" and must be converted before doing maths. → num = int(input("Number: "))
Q5
a = input("A: ") b = input("B: ") print(a + b) # Test: type 3 and 4, expect 7 → input() returns text, so "3" + "4" joins the strings and prints 34, not 7. The actual output will not match the expected result.
Q6
# Test plan: type 10, then 2 name = input("Name: ") age = input("Age: ") # Tester types 2, then 10 → The tester typed the inputs in a different order from the plan, so the values went into the wrong variables and the test is not repeatable.
Section C: Definitions
Q1
input() — Pauses a program so you can type a test value; it returns text. Test by typing each planned input in order, then compare the printed output with the expected result.
Section D: Fill in the Blanks
Q1
age = input("Enter your age: ") print(age)
Q2
n = int(input("Number: ")) print(n * 2)
Q3
a = input("First: ") b = input("Second: ") print(int(a) + int(b))
Q4
x = input("Enter a number: ") print(type(x))
Q5
actual = "Hi " + input("Name: ") print(actual == "Hi Sam")
Section E: Spot the Error
Q1
C — input() returns text, so a + b joins the strings. Typing 2 then 3 prints 23 instead of the expected 5. Convert with int(a) + int(b).
Q2
D — passed has no quotes, so Python looks for a variable called passed and gives a NameError. The text must be in quotes: "passed".
Q3
B — input needs brackets to be called. Without () the program never pauses for the test value. It should be int(input()).
Q4
C — The total should multiply price by quantity. Typing 2.5 then 4 should print 10.0 but the broken line prints 6.5. It should be p * q.
Q5
C — A single = assigns a value and is a syntax error in a condition. To compare the typed test input with the secret, use ==.