从第一个值中减去列表中的值 (Python)

Subtract values in a list from the first value (Python)

我有一个整数列表,我想遍历列表中的每个数字并从第一个数字中减去它,就像计算器如何进行多输入减法一样。

myList = [3,2,1,4,5]


def subtractionDifference(n):
  start = n[0]

  for num in n[1:]:
      difference = start - num
  return difference

print(subtrationDifference(myList))

这会打印 -2,理想情况下它会打印 -9

试试这个

myList = [3,2,1,4,5]

def subtractionDifference(n):
  difference = n[0]

  for num in n[1:]:
      difference = difference - num
  return difference

print(subtrationDifference(myList))