Search code examples
pythonsortingtypeerrorcomplexity-theoryinsertion

TypeError: input() takes 0 positional arguments but 1 was given


I am writing a Python program to measure the time complexity of insertion sort. However, I got the above mentioned error on line number 6. This error also occurs during other{ int(inputs) }. Any help would be great,thanks. My code is:

import random, matplotlib.pyplot as plt

def input():
    arr=[]
    ret_size=[]
    ret_count=[]
    n=int(input('enter the number of trials'))
    for i in range(n):
        x=int(input('size of array:'))
        for z in range(x):
            r=random.randint(1,2000)
            arr.append(r)
        count=0
        for ind in range(1,len(arr)):
            count+=1
            cur=arr[ind]
            count+=1
            pos=ind
            while pos>0 and arr[pos-1]>cur:
                count+=1
                pos=pos-1
                count+=1
            arr[pos]=cur
        count+=1
        print('sorted listL')
        print(arr)
        print('number of hops:')
        print(count)
        ret_size.append(x)
        ret_count.append(count)
    plt.plot(ret_size,ret_count)
    plt.xlabel('size of input')
    plt.ylabel('number of hops')
    plt.title('insertion sort')
    plt.show()

input()


Solution

  • Note these 2 lines of your code:

    def input():
    

    and

        n=int(input('enter the number of trials'))
    

    In the first of them you redefined the built-in function input() (which accepts 0 or 1 parameter) with your own with the same name (which accepts only 0 parameters, i. e. none).

    So in the second of them you called NOT the built-in function input() - as you wanted - but your own one, and as you callded it with 1 parameter ('enter the number of trials'), you got a relevant error.

    Choose an other name for defining your input() function - and then use that name for calling it, too.