mylist code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • imran akhtar
    New Member
    • Nov 2008
    • 64

    mylist code

    i have to Write a program which outputs the alternating sum of all elements of a list taken in as a parameter. For example if myList contains 4, 7, 9, 23, 45, 12, the function should output the results of 4 + 7, 9 – 23, 45 + 12,


    i dont understand how i would start this, if any one can give me help, how to start this that will be great.
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    Please check my reply in your other thread.

    MODERATOR

    Comment

    • imran akhtar
      New Member
      • Nov 2008
      • 64

      #3
      my code list

      ok understand, wht you have siad, below i have givin it a go, can you direct me from here.

      Code:
      def listtotal ():
          total=0
          mylist=[4,7,9,23,45,12]
          for x in range (len(mylist)):
      
      temp=input("plese enter a number")
              if temp in mylist:
                  print "number in list"
              else:
                  print "numbers not in list"
                  
              total=total+temp
              return total

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Originally posted by imran akhtar
        i have to Write a program which outputs the alternating sum of all elements of a list taken in as a parameter. For example if myList contains 4, 7, 9, 23, 45, 12, the function should output the results of 4 + 7, 9 – 23, 45 + 12,


        i dont understand how i would start this, if any one can give me help, how to start this that will be great.
        According to the desired output, the result should be a list (the result of 3 calculations). You can iterate on mylist with the range function using a step of 2. Example:
        Code:
        mylist = [4,7,12,45,19,6]
        factor = 1
        results = []
        for i in range(0, len(mylist), 2):
            results.append(mylist[i] + mylist[i+1]*factor)
            factor *= -1

        Comment

        Working...