Exercise
Data Organization
Objective
Develop a Python program to prompt the user for 10 integer numbers (ranging from -1000 to 1000), sort them, and display them in sorted order.
Example Python Exercise
Show Python Code
# Create an empty list to store the integers
numbers = []
# Prompt the user for 10 integers
for i in range(10):
while True:
try:
num = int(input(f"Enter integer number {i+1} (between -1000 and 1000): "))
if -1000 <= num <= 1000:
numbers.append(num)
break
else:
print("Please enter a number between -1000 and 1000.")
except ValueError:
print("Invalid input. Please enter an integer.")
# Sort the numbers in ascending order
numbers.sort()
# Display the sorted numbers
print("The sorted numbers are:")
for num in numbers:
print(num)
Output
Enter integer number 1 (between -1000 and 1000): 300
Enter integer number 2 (between -1000 and 1000): 100
Enter integer number 3 (between -1000 and 1000): -200
Enter integer number 4 (between -1000 and 1000): 500
Enter integer number 5 (between -1000 and 1000): 600
Enter integer number 6 (between -1000 and 1000): 0
Enter integer number 7 (between -1000 and 1000): -50
Enter integer number 8 (between -1000 and 1000): -100
Enter integer number 9 (between -1000 and 1000): 400
Enter integer number 10 (between -1000 and 1000): 200
The sorted numbers are:
-200
-100
-50
0
100
200
300
400
500
600
Share this Python Exercise