SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Y Daniel Lang
Chapter:
0.lists
Exercise:
Programming Excercises
Question:2 | ISBN:978013274719 | Edition: 6

Question

(Reverse the numbers entered) Write a program that reads a list of integers and displays them in the reverse order in which they were read.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Reverse the numbers entered Program Code:

# Prompt the user to enter a list of integers
numbersInput = input("Enter a list of integers (space-separated): ").split()

# Convert the input strings to integers
numbers = [int(num) for num in numbersInput]

# Reverse the list using list slicing
reversedNums = numbers[::-1]

# Display the reversed list
print("Reversed numbers:", reversedNums)

Executed Output 1:

Enter a list of integers (space-separated): 5 10 15 20 25 30 45 55
Reversed numbers: [55, 45, 30, 25, 20, 15, 10, 5]

Executed Output 2:

Enter a list of integers (space-separated): 100 5 25 13 99 77 66 121
Reversed numbers: [121, 66, 77, 99, 13, 25, 5, 100]

 

0 0

Discussions

Post the discussion to improve the above solution.