Loops Practice

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.1 Answer

This program receives a positive number as input, and then will check to see if that number is evenly divisible by the numbers $ 1 $ through $ 9 $. If so, it will print that number.

For example, if the input provided is $ 60 $, then the output should be:

1
2
3
4
5
6

since $ 60 $ is divisible by the first $ 6 $ integers.

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.2 Answer

There are many ways to answer this. A quick and easy way is to simply provide the numbers $ 1 $ through $ 9 $ as inputs. In the previous answer, we already know that $ 60 $ covers six of the nine possible outputs, so two or three additional inputs can cover the rest.

Mathematically, the number $ 2520 $ is the lowest common multiple of the first ten integers, so that single value as input will produce all nine possible outputs.

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.3 Answer

Simply put, this program will convert any positive integer into its binary representation. For example, the input $ 42 $ will generate the output 101010, which is in binary is the value $ 42 $.

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.4 Answer

To achieve full coverage, a sufficient set of inputs would be something similar to:

  • 0
  • 1
  • 2

That should be enough to exercise all possible code branches and paths. However, in order to truly determine if the program is producing the correct output, it would be useful to choose a couple of larger inputs. Some good choices would be numbers near a power of $ 2 $, such as:

  • 15
  • 16

Many other answers are possible here.

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.5 Answer

One possible solution is given below:

x = int(input("Enter a positive integer greater than 5: "))
while(x <= 5):
    print("Invalid Input!")
    x = int(input("Enter a positive integer greater than 5: "))
a = 1
b = 1
while b < x:
    c = a + b
    a = b
    b = c
if b == x:
    print("In sequence")
else:
    print("Not in sequence")

There are many other valid approaches as well.

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?

5.5 Answer

One possible solution is given below:

x = int(input("Enter an integer between 1 and 100: "))
while(x < 1 or x > 100):
    print("Invalid Input!")
    x = int(input("Enter an integer between 1 and 100: "))
first = x
x = int(input("Enter an integer between 0 and 9: "))
while(x < 0 or x > 9):
    print("Invalid Input!")
    x = int(input("Enter an integer between 0 and 9: "))
second = x
for i in range(first, first * 30 + 1, first):
    if i % 10 == second:
        print(i)

This solution uses the range() function directly to compute the multiples. It is also possible to use range() to generate a list of numbers from $ 1 $ to $ 30 $ and compute the multiples using those values.