By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,621 Members | 1,101 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,621 IT Pros & Developers. It's quick & easy.

mylist code

P: 64
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.
Dec 20 '08 #1
Share this Question
Share on Google+
3 Replies


Curtis Rutland
Expert 2.5K+
P: 3,256
Please check my reply in your other thread.

MODERATOR
Dec 20 '08 #2

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

Expand|Select|Wrap|Line Numbers
  1. def listtotal ():
  2.     total=0
  3.     mylist=[4,7,9,23,45,12]
  4.     for x in range (len(mylist)):
  5.  
  6. temp=input("plese enter a number")
  7.         if temp in mylist:
  8.             print "number in list"
  9.         else:
  10.             print "numbers not in list"
  11.  
  12.         total=total+temp
  13.         return total
Dec 20 '08 #3

bvdet
Expert Mod 2.5K+
P: 2,851
@imran akhtar
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:
Expand|Select|Wrap|Line Numbers
  1. mylist = [4,7,12,45,19,6]
  2. factor = 1
  3. results = []
  4. for i in range(0, len(mylist), 2):
  5.     results.append(mylist[i] + mylist[i+1]*factor)
  6.     factor *= -1
Dec 20 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.