-1

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()

MarianD
  • 13,096
  • 12
  • 42
  • 54
  • 2
    `input` is a built-in function. Its a very bad idea to use that as a name for your own function. What is `n=int(input('enter the number of trials'))` going to resolve to when you've called you own function `input`? Rename your function and it will fix this issue. – roganjosh Feb 02 '19 at 17:53
  • 1
    You defined your own `input()` function. You can't now use the built-in `input()` function, not without some additional work. Easier to just rename your own function. – Martijn Pieters Feb 02 '19 at 17:56
  • oh ! thank you , did not notice that. – ABHISHEK PARMAR Feb 02 '19 at 17:57
  • wait, how do you use the builtin after shadowing it? @MartijnPieters i..uh...asking for a friend. – Paritosh Singh Feb 02 '19 at 17:57
  • yes it is working now thank you – ABHISHEK PARMAR Feb 02 '19 at 17:59
  • 2
    @ParitoshSingh: 3 options: create an alias up front (`builtin_input = input` before binding to `input`), by [importing the `builtins` module](https://docs.python.org/3/library/builtins.html) (`import builtins`, ..., `builtins.input()`), or by deleting the global that shadows the built-in (`del input`, not an option here). – Martijn Pieters Feb 02 '19 at 18:07
  • Much appreciated – Paritosh Singh Feb 02 '19 at 18:08

1 Answers1

1

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.

MarianD
  • 13,096
  • 12
  • 42
  • 54