| | |
plz help me with for loop thx
![]() |
•
•
Join Date: Apr 2008
Posts: 4
Reputation:
Solved Threads: 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
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
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:
3. List indexing.
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)
range(6) => [0, 1, 2, 3, 4, 5] range(0, 6, 2) => [0, 2, 4]
Last edited by jaux; Apr 25th, 2008 at 3:40 am.
![]() |
Similar Threads
- Functions and while loops homework help (C++)
- Need Help >> I Give Up , Cant Do This Program :'((( (Java)
- delete first line from a text file (C++)
- Initializing Multidimensional Arrays plz help! (C++)
- Loop challenge (C)
- A Small Problem ....plz Help Me Out.... (C)
Other Threads in the Python Forum
- Previous Thread: Regarding "popen2" module
- Next Thread: Infinite loop
| Thread Tools | Search this Thread |
abrupt ansi anti apache approximation array assignment avogadro backend beginner binary bluetooth book builtin calculator character code converter countpasswordentry curved customdialog dan08 dictionaries dictionary dynamic examples exe file float format function gnu graphics gui heads homework ideas import inches input java launcher library line lines linux list lists loop mouse mysqlquery number numbers numeric output parsing path phonebook plugin pointer port prime programming progressbar projects py2exe pygame python random recursion redirect scrolledtext software statictext statistics string strings sum table terminal text textarea thread threading time tlapse trick tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable wordgame write wxpython xlib





