Objective
Develop a Python program that prompts the user for two numbers and displays the numbers between them (inclusive) two times using "for" and "while" loops.
Enter the first number: 6
Enter the last number: 12
6 7 8 9 10 11 12
6 7 8 9 10 11 12
6 7 8 9 10 11 12
Example Python Exercise
Show Python Code
#Using "for"
# Prompt the user to enter the first number
start = int(input("Enter the first number: "))
# Prompt the user to enter the last number
end = int(input("Enter the last number: "))
# Use a for loop to display the numbers between start and end (inclusive)
for i in range(start, end + 1):
print(i, end=" ")
print() # Move to the next line
#Using "while"
# Prompt the user to enter the first number
start = int(input("Enter the first number: "))
# Prompt the user to enter the last number
end = int(input("Enter the last number: "))
# Initialize the counter variable
i = start
# Use a while loop to display the numbers between start and end (inclusive)
while i <= end:
print(i, end=" ")
i += 1
print() # Move to the next line
Output
Enter the first number: 6
Enter the last number: 12
6 7 8 9 10 11 12
6 7 8 9 10 11 12
6 7 8 9 10 11 12
Share this Python Exercise