0

2. List Processing (10 Points) Write a program that asks the user to enter a list with an even numberof integers. The program then has to construct a new list by adding pairs of integers in the list. Hereis an example of a program run, where the user enters
the list [2,7,8,-3,11,-22]
Enter a list with an even number of integers: [2,7,8,-3,11,-22]
The new list is [9, 5, -11]
Note that the first integer (9) in the new list is the sum of the first two integers (2+7=9) in the inputlist, the second number (5) the sum of the next two integers (8+(-3)=5) and so on. You can safelyassume that the user enters a correct list with an even number of integers. Note that such a list starts with ‘[’ and ends with ‘]’, i.e., both characters need to be entered by the user. Use a for loop and the+ operation to concatenate lists and list elements for constructing the new list (note that you have to initialize such a new list before you construct it in the for-loop).

here is my instruction, but i have no clue to start with. can anyone help me with for loop? thank you very much

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by jaux
0

The following hints might be helpful:
0. raw_input will ask user for input.
1. eval will construct a list from user input (string) for you very easily.
2. Since for loop is required, range function seems to be handy:

range(6) => [0, 1, 2, 3, 4, 5]
range(0, 6, 2) => [0, 2, 4]

3. List indexing.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.