""" I added some debugging printing to better illustrate the problem. Changing len(n) to len(x) was the first step. You need to change all references to 'n' in the function to 'x' The function was working on the variable 'n' that you defined outside of the function (the global variable) instead of the local copy of 'x' that only exists within the function call itself. """ n = [3, 5, 7] def double_list(x): for i in range(0, len(n)): n[i] = n[i] * 2 return n print "\nRunning original code...\n" print "double_list(n) = %s" % double_list(n) print "double_list([1,2,3]) = %s" % double_list([1,2,3]) print "double_list(range(0,10)) = double_list(%s) = %s" % (range(0,10), double_list(range(0,10))) print "double_list([]) = %s" % double_list([]) n = [3, 5, 7] def double_list(x): for i in range(0, len(x)): x[i] = x[i] * 2 return x print "\nRunning revised code...\n" print "double_list(n) = %s" % double_list(n) print "double_list([1,2,3]) = %s" % double_list([1,2,3]) print "double_list(range(0,10)) = double_list(%s) = %s" % (range(0,10), double_list(range(0,10))) print "double_list([]) = %s" % double_list([])
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question