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

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

我正在编写一个Python程序来测量插入排序的时间复杂度。但是,我在第 6 行遇到了上述错误。此错误也发生在 other{int(inputs)} 期间。任何帮助都会很棒,谢谢。我的代码是:

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

注意你的代码中的这两行:

def input():

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

在其中的第一个中,您 重新定义了 内置函数 input()(接受 01 参数)拥有同名(只接受0参数,即none)。

所以在第二个中你调用了 NOT 内置函数 input() - 如你所愿 - 但 你自己的 ,当你用 1 参数调用它时 ('enter the number of trials'),你得到了一个相关的错误。

选择一个其他名称 来定义您的 input() 函数 - 然后也使用该名称来调用它。