We have an assignment in my beginning Python class and I am completely lost on it. I have asked some students and most of them were lost and the teacher gave me a very vague answer that did not help me at all.
THE ASSIGNMENT
"Write a function that is passed in a list and 2 index numbers (your function can assume these index numbers are between 0 and len() - 1, inclusive). Your function should swap the values at those index locations.
For example:
stuff = [10, 20, 30, 40]
swap(stuff, 0, 2)
print stuff
Would display:
[30, 20, 10, 40]"
I have no idea what i'm doing wrong but I'm getting
[10,10,30,40]
MY CODE:
def swap(list, indexA, indexB):
result =[]
sub=len(list)-1
for item in list:
if item == list[indexA]:
item=list[indexB]
if item == list[indexB]:
item=list[indexA]
result.append(item)
return result
def main():
test=[10,20,30,40]
print swap(test,0,1)
main()
----
I'm not trying to cheat and get my homework done by someone else, so can someone just explain what I am doing wrong or give me a much needed hint to point me in the right direction?
Thank you.
|
Dave |
The problem is: |
Other Questions & Answers
|