# Why the code gives an error "list assignment index out of range"?

Decided to see how you can do sorting of arrays in python. Found the program, understood it, but to integrate with other code does not work, then began to partially migrate this code. After transferring the pattern gets the error "list assignment index out of range". What to do?
The code itself:
``````def quicksort(array):
if len(array) < 2:#Base case: arrays with 0 or 1 element is already sorted
return array
else:
pivot=array#Recursive Case
#print("Reference pivot item --> ",pivot)
less = [i for i in array[1:] if i <= pivot]#Subarray all elements smaller than the reference
#print("Less support --> less",less)
greater = [i for i in array[1:] if i > pivot]#Subarray all elements of the large supporting
#print("Larger reference --> greter",greater)
return quicksort(less) + [pivot] + quicksort(greater)

i = 0
n = int(input('Enter number of numbers to sort: '))
A = [n]
for i in range(0,n) :
i = int(i)
A[i]=input('Enter number: ')
i+=1
print(quicksort(A))``````
April 3rd 20 at 18:53
April 3rd 20 at 18:55
Solution
A[i]= used for existing members of the list if i outside long is causing the error
``````n = int(input('Enter number of numbers to sort: '))
A = [n]
A += [int(input('Enter number a[%d] = '%(i+1))) for i in range(0,n)]
print(quicksort(A))``````
Alright, thanks, that is necessary in a cycle to add another member? - alan commented on April 3rd 20 at 18:58
Thank you very much, a little completed the cycle and everything now works) - alan commented on April 3rd 20 at 19:01
@karine_Erdman ie do not mind that A = [n] this code does not make sense. - katlyn.Pfeffer81 commented on April 3rd 20 at 19:04
I just PascalABC switched to python and some of the things that worked in Pascal stumble institutions.
I realized my mistake, sincerely apologize for the stupid and obvious question and I wish you well) - alan commented on April 3rd 20 at 19:07
April 3rd 20 at 18:57
1. understand what makes A = [n]
2. and why would you i = int(i) and i+=1
how to work with lists you never studied
Paragraph 2 - remains of Attila while I removed them and nothing has changed
and without A = [n] code almost at the beginning of breaks and the same error comes out - alan commented on April 3rd 20 at 19:00
``````Traceback (most recent call last):
File "һ:/ргодгам/19.12.19/2алгоритм quick sort massiver", line 19, in <module>
A[i]=input('Enter number: ')
NameError: name 'A' is not defined``````

here is the full description of the error - alan commented on April 3rd 20 at 19:03
@Osvaldo.Champlin, @Osvaldo.Champlin,
``````File "һ:/ргодгам/19.12.19/2алгоритм quick sort massiver", line 20, in <module>
A[i]=input('Enter number: ')
IndexError: list assignment index out of range``````

here, I accidentally deleted the variable And - alan commented on April 3rd 20 at 19:06
@Osvaldo.Champlin, I did not say remove, I asked you to make out what makes this design. So why don't you just start reading a book on Python, Lutz, for example, and then all you do is at random. - katlyn.Pfeffer81 commented on April 3rd 20 at 19:09
@Maybell.Miller, well, using the method of spear and you can learn)
Because if you do not make mistakes, then you are not going to understand how to construct language and how not to do) - alan commented on April 3rd 20 at 19:12
I agree with @Casey.Miller - here we see a classic at random. - Ashton_Kreiger97 commented on April 3rd 20 at 19:15

Find more questions by tags Python