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

Get item of a list by index

P: 6
Hey,
I made a programm that looks like this, but I have a problem.
Expand|Select|Wrap|Line Numbers
  1. command = raw_input(">")
  2. fullcommand = command.split(" ")
  3. #example user input: >add 10 15
  4. if fullcommand.index("add") == 0:
  5. #how I want the rest to work:
  6. * * *a= fullcommand.getItem(1)
  7. * * *b= fullcommand.getItem(2)
  8. * * *c= a + b
  9. * * *print ("Result: *" + c)
  10.  
So what I need is a function that gets the item of a list which has the index 1 and the item with the index 2.
How does that work?
Peter
Feb 4 '12 #1

✓ answered by Smygis

Expand|Select|Wrap|Line Numbers
  1. command = raw_input(">")
  2. fc = command.split()
  3. if fc[0] == "add":
  4.     a = fc[1]
  5.     b = fc[2]
  6.     c = a + b
  7.     print "Result: " + c
Does exactly what you described that it should do. (I'm using python 2.7, you'll have to add parentheses at the print function if you are using python 3.x)

Share this Question
Share on Google+
1 Reply

Smygis
100+
P: 126
Expand|Select|Wrap|Line Numbers
  1. command = raw_input(">")
  2. fc = command.split()
  3. if fc[0] == "add":
  4.     a = fc[1]
  5.     b = fc[2]
  6.     c = a + b
  7.     print "Result: " + c
Does exactly what you described that it should do. (I'm using python 2.7, you'll have to add parentheses at the print function if you are using python 3.x)
Feb 4 '12 #2

Post your reply

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