First, a little context: I'm learning python (I'm using 2.7.9) and trying to do an exercise where I have to first take a single number which represent the number of my test cases. After that, each test case is a string which contains a series of numbers "2 3 5..." so on. Since it is a pain to convert a single string containing numbers to a list of integers, I created a function named "stringsToInts" to do this for me:
def stringsToInts(words, pNumbers = []):
for word in words:
pNumbers.append(int(word))
return pNumbers
print "data:"
testCases = int(raw_input())
numbers = []
while testCases > 0:
numbers = stringsToInts(raw_input().split(' '))
testCases -= 1
print "answer:"
for number in numbers:
print number,
However, I noticed something weird. If I define the integer list as an optional argument in my function, this is what happens:
(my input in the command line)
data:
2
2 3 4
6 7 8
(my output)
answer:
2 3 4 6 7 8
(What I expected)
answer:
6 7 8
Since I only print numbers
, why does the list keeps the numbers from all my test cases? What I'm doing wrong?