Get item of a list by index

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Peter Kr
    New Member
    • Feb 2012
    • 6

    Get item of a list by index

    Hey,
    I made a programm that looks like this, but I have a problem.
    Code:
    command = raw_input(">")
    fullcommand = command.split(" ")
    #example user input: >add 10 15
    if fullcommand.index("add") == 0:
    #how I want the rest to work:
    * * *a= fullcommand.getItem(1)
    * * *b= fullcommand.getItem(2)
    * * *c= a + b
    * * *print ("Result: *" + c)
    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
    Last edited by Peter Kr; Feb 4 '12, 09:25 AM. Reason: Made a mistake in the code
  • Smygis
    New Member
    • Jun 2007
    • 126

    #2
    Code:
    command = raw_input(">")
    fc = command.split()
    if fc[0] == "add":
        a = fc[1]
        b = fc[2]
        c = a + b
        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)

    Comment

    Working...