943,800 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 494
  • Python RSS
Apr 25th, 2008
0

plz help me with for loop thx

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
urmybaby is offline Offline
4 posts
since Apr 2008
Apr 25th, 2008
0

Re: plz help me with for loop thx

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:
python Syntax (Toggle Plain Text)
  1. range(6) => [0, 1, 2, 3, 4, 5]
  2. range(0, 6, 2) => [0, 2, 4]
3. List indexing.
Last edited by jaux; Apr 25th, 2008 at 3:40 am.
Reputation Points: 10
Solved Threads: 2
Newbie Poster
jaux is offline Offline
15 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Regarding "popen2" module
Next Thread in Python Forum Timeline: Infinite loop





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC