plz help me with for loop thx

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2008
Posts: 4
Reputation: urmybaby is an unknown quantity at this point 
Solved Threads: 0
urmybaby urmybaby is offline Offline
Newbie Poster

plz help me with for loop thx

 
0
  #1
Apr 25th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 15
Reputation: jaux is an unknown quantity at this point 
Solved Threads: 2
jaux's Avatar
jaux jaux is offline Offline
Newbie Poster

Re: plz help me with for loop thx

 
0
  #2
Apr 25th, 2008
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:
  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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC