Hello,
i am having trouble writing this program on Python. Write a program that asks the user to enter a list with an even number of integers. The program then has to construct a new list by adding pairs of integers in the list. I have been working on this for a few hours and the closest i got was
Enter a list with an even number of integers:: [2,7,8,-3,11,-22]
[9] [15] [5] [8] [-11]
but i am suppose to have [9, 5, -11].
Any help would be greatly appreciated. Thanks in advance
5 1455 bvdet 2,851
Expert Mod 2GB
Hello,
i am having trouble writing this program on Python. Write a program that asks the user to enter a list with an even number of integers. The program then has to construct a new list by adding pairs of integers in the list. I have been working on this for a few hours and the closest i got was
Enter a list with an even number of integers:: [2,7,8,-3,11,-22]
[9] [15] [5] [8] [-11]
but i am suppose to have [9, 5, -11].
Any help would be greatly appreciated. Thanks in advance
- [num_list[i]+num_list[i+1] for i in range(0,len(num_list),2)]
hi, i was able to get the program to work for the most part. However, my result is this
Enter a list with an even number of integers: [2,7,8,-3,11,-22]
The new list is [9] The new list is [5] The new list is [-11]
while i want it to be like this
The new list is [9, 5, -11]
here is the script:
num_list = input("Enter a list with an even number of integers: ")
for i in range(0,len(num_list),2):
P = [num_list[i] + num_list[i+1]]
print "The new list is ", P,
Thanks for the help
hi, i was able to get the program to work for the most part. However, my result is this
Enter a list with an even number of integers: [2,7,8,-3,11,-22]
The new list is [9] The new list is [5] The new list is [-11]
while i want it to be like this
The new list is [9, 5, -11]
here is the script:
num_list = input("Enter a list with an even number of integers: ")
for i in range(0,len(num_list),2):
P = [num_list[i] + num_list[i+1]]
print "The new list is ", P,
Thanks for the help
* Please use CODE tags (without spacing inside brackets) around your code so that your posts are friendlier to read: - [ CODE=python ]..code goes here..[ /CODE ]
You are printing inside the loop. Also you are not doing anything to save those entries in the list P. All you need to do is take the print statement out of the loop, declare P as a list before the loop, and make it P += instead of P = .
Hope that helps.
Also, you should use the modulo operater (%) to check that the length of the list really is even.
yes i got it! it is amazing how a small change can make or break the program. Thanks for the help.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: fudmore |
last post by:
Hello Everybody.
I have a Segmentation fault problem. The code section at the bottom
keeps throwing a Segmentation fault when it enters the IF block for the
second time.
const int...
|
by: Harry Overs |
last post by:
My program needs to take a pointer to BYTE array (unsigned char*) and
convert it into a STL list so that each BYTE in the array has its own
element in the list, i.e. if the array has hundred bytes...
|
by: koperenkogel |
last post by:
Dear cpp-ians,
I am writing code for a image segmentation program. For this purpose I
want to make a list (or array) of all pixels and take a random pixel
out of the list. Once the pixel is...
|
by: tom |
last post by:
I am new to SQL administration.
>From a list of IDs that are the primary key in one table (i.e. Customer
Table), I want to make changes in tables that use those IDs as a
foreign key.
...
|
by: soren juhu |
last post by:
Hi,
I am developing a C Program for reading over a million files of size 1
kilobytes each and sending the contents to another program using some
middle ware. I need some help on designing the...
|
by: Ganapathy |
last post by:
I have COM dll code written in VC 6.0. When i tried
compiling this code in VC 7, The MIDL cmpiler gets called
twice. i.e. it initially compiles fully & immediately a
line - 64 bit processing'...
|
by: Madison Kelly |
last post by:
Hi all,
I am new to the list and I didn't want to seem rude at all so I
wanted to ask if this was okay first.
I have a program I have written in perl which uses a postgresSQL
database as the...
|
by: Kriston-Vizi Janos |
last post by:
Dear Mr. Kern, and Members,
Thank you very much for the fast answer, my question became
over-simplified.
My source code is appended below. It uses two text files (L.txt and
GC.txt) as input...
|
by: Jack |
last post by:
Hi,
I have a asp form where one element is a list box which lists four years
starting from 2004. This list is drawn from a database table which has YearID
and Year as two fields as shown below:...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |