226k views
3 votes
Objective: Learn Repetition Structures using the Python programming language

You are not required to submit pseudocode with this lab.
You can use any previous structures.
You must use a for in range and while loop.
Do not use lists or functions.
Do not use the break statement to exit a loop.
Specification:
Create a program that displays a range of numbers, based on 3 integer number entries by the user. These make up the start, stop and step of the number range.
The user will input 3 numbers in this order:
First input is the starting number.
Second input is the ending number.
Third input is the step number.
Check that all number inputs are between 1 and 30. If they are not, display an error and ask the user to re-enter with a valid number. Also, make sure the starting number is always less than the ending number.
Example:
For the following inputs: 1, 10, 2 the output would be:
1
3
5
7
9

1 Answer

5 votes

Final answer:

The task is to create a Python program that uses a for loop and while loop to display a range of numbers with proper input validation. The program will ensure all inputs are within the range of 1 to 30, with the starting number being less than the ending number.

Step-by-step explanation:

The challenge presented is to write a Python program that uses a for loop and a while loop to display a range of numbers. Input validation is required to ensure that the starting number is less than the ending number and that all numbers are between 1 and 30. If the user provides invalid input, an error message should prompt them to try again. Here is an example of how this could be done:

start = int(input('Enter the starting number (between 1 and 30): '))
while start not in range(1, 31):
print('Error: The number must be between 1 and 30.')
start = int(input('Please enter the starting number: '))

stop = int(input('Enter the ending number (between 1 and 30, greater than start): '))
while stop not in range(1, 31) or stop <= start:
print('Error: The number must be between 1 and 30 and greater than the starting number.')
stop = int(input('Please enter the ending number: '))

step = int(input('Enter the step number (between 1 and 30): '))
while step not in range(1, 31):
print('Error: The number must be between 1 and 30.')
step = int(input('Please enter the step number: '))

for number in range(start, stop, step):
print(number)

This code snippet prompts the user for the starting, ending, and step numbers, with input validation checks in place. It then proceeds to use a for loop with the range function to generate the required sequence of numbers, which are printed line by line.

User Shea Hunter Belsky
by
8.3k points