无法返回更新的全局变量

时间:2017-04-24 19:03:42

标签: python-2.7

我是使用2.7版本的绝对python初学者。 这是我的全局变量' total1'

的代码
total1 = {}
def process(totals,trans):
  global total1 

  if trans[0] == 'receive':
      total1[trans[1]] = totals[trans[1]] + trans[2]
  elif trans[0] == 'ship':
      total1[trans[1]] = totals[trans[1]] - trans[2]
  return total1

#'totals' and 'trans' are dict and list of strings respectively.
#I wanted to get an updated 'total1' dict.
#For example if 
      #totals = {'a':10}
      #and trans = ['ship', 'a',3]
      #I want total1 to be {'a':7}
#However i keep getting an empty dict {},
#How can I modify my code?

#Thanks!
#edited version
#while loop deleted

0 个答案:

没有答案