List Processing program help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • clouddragon
    New Member
    • Apr 2008
    • 6

    List Processing program help

    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
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by clouddragon
    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
    [code=Python][num_list[i]+num_list[i+1] for i in range(0,len(num _list),2)][/code]

    Comment

    • clouddragon
      New Member
      • Apr 2008
      • 6

      #3
      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

      Comment

      • jlm699
        Contributor
        • Jul 2007
        • 314

        #4
        Originally posted by clouddragon
        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:
         [ 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.

        Comment

        • woooee
          New Member
          • Mar 2008
          • 43

          #5
          Also, you should use the modulo operater (%) to check that the length of the list really is even.

          Comment

          • clouddragon
            New Member
            • Apr 2008
            • 6

            #6
            yes i got it! it is amazing how a small change can make or break the program. Thanks for the help.

            Comment

            Working...