Let’s try some simple practice problems. These problems are not graded - they are just for you to practice before doing the real exercises in the lab itself. You can find the answers below each question by clicking the button below each question.
5.1 Reading Code
Consider the following pseudocode program:
x = int(input("Enter a positive integer: "))
while(x <= 0):
print("Invalid Input!")
x = int(input("Enter a positive integer: "))
a = 1
for i in range(9):
if x % a == 0:
print(f"{a}")
a = a + 1
Explain, in your own words, the output that this program produces in relation to the input provided.
A fully correct answer is a succinct description of the output as it relates to the input. A partially correct answer is a step-by-step description of each line in the program and the output it will produce based on the input.
5.2 Testing Code
Consider the program above. Devise a set of inputs that will cause each of the 9 possible numerical outputs to print at least once, and for each input, list the numbers that will be printed as output.
5.3 Reading Code
Consider the following Python program:
x = int(input("Enter a positive integer: "))
while(x <= 0):
print("Invalid Input!")
x = int(input("Enter a positive integer: "))
a = ""
while x > 0:
if x % 2 == 0:
a = "0" + a
else:
a = "1" + a
x = x // 2
print(a)
Explain, in your own words, the output that this program produces in relation to the input provided.
A fully correct answer is a succinct description of the output as it relates to the input. A partially correct answer is a step-by-step description of each line in the program and the output it will produce based on the input.
Hint: when numbers are stored digitally on a computer, what numerical format is used?
5.4 Testing Code
Consider the Python program above. Devise a set of inputs that you feel are sufficient to test the program, and briefly explain why those inputs are sufficient.
Hint: Achieving coverage is very simple, but providing enough input to be confident that the program is generating the correct output should also be considered.
5.5 Writing Code
Write a complete Python program that meets the specification below.
The Fibonacci Sequence is a sequence of numbers where the next number is the sum of the previous two numbers. The sequence starts with the numbers $ 1 $ and $ 1 $, making the next number $ 2 $. The following number is $ 3 $, since it is the sum of the second and third number of the sequence. The sequence continues indefinitely.
For this program, ask the user to input a number that must be greater than $ 5 $. If the user inputs a number that is $ 5 $ or less, display an error and prompt the user for another input until a valid input is received.
Then, determine if the number provided as input is part of the Fibonacci Sequence. This can be done by computing the sequence one number at a time until the desired number is reached or passed. Print either "In sequence"
or "Not in sequence"
depending on the result found.
5.6 Writing Code
Write a complete Python program that meets the specification below.
Write a program that will print the first $ 30 $ multiples of a given number, but only multiples where the last digit is equal to a second given number.
For this program, ask the user to provide two integers - the first between $ 1 $ and $ 100 $, inclusive, and the second between $ 0 $ and $ 9 $, inclusive. If the user provides an invalid value for either input, print an error message and prompt the user for another input until a valid input is received.
Then, the program should compute the first $ 30 $ multiples of the first given number. This would be equivalent to multiplying the number by $ 1 $ all the way through multiplying the number by $ 30 $. Then, if that computed multiple ends with the same digit as the second number, it should be printed. Otherwise, no output is produced for that number.
For example, if the first given number is $ 9 $ and the second number is $ 1 $, then the output should be:
81
171
261
Your program should make use of a for loop and the Python range()
function.
Hint: what is the value of 81 % 10
? What about 27 % 10
? Does that value help you determine whether the number should be printed or not?