| | |
plz help me with for loop thx
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
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 |
alarm assignment avogadro beginner bluetooth character cmd code customdialog cx-freeze data decimals dictionary directory dynamic error examples exe file float format function generator gnu graphics gui halp homework http ideas import input itunes java leftmouse line linux list lists logging loop maintain maze module mouse mysqldb number numbers output parsing path port prime programming projects push py2exe pygame pyglet pyqt python queue random recursion schedule screensaverloopinactive script scrolledtext slicenotation sqlite ssh stdout string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode urllib urllib2 variable variables ventrilo verify vigenere web webservice wikipedia windows wxpython xlib





