按索引获取列表项

嘿我做了一个看起来像这样的程序,但我有一个问题。

选择 | 换行 | 行号
  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.  

所以我需要的是一个函数,它获取索引为1的列表项和索引为2的列表项。这是如何工作的?彼得

# 回答1

选择 | 换行 | 行号
  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

完全按照你描述的那样做。(我使用的是python 2.7,如果使用python 3.x,则必须在print函数中添加括号)

标签: python

添加新评论