Python TypeError:input()接受0个位置参数,但给出了1个
我正在编写一个Python程序来测量插入排序的时间复杂性。然而,我在第6行得到了上面提到的错误。在其他{int(输入)}期间也会发生此错误。任何帮助都会很好,谢谢。我的代码是:Python TypeError:input()接受0个位置参数,但给出了1个,python,sorting,typeerror,complexity-theory,insertion,Python,Sorting,Typeerror,Complexity Theory,Insertion,我正在编写一个Python程序来测量插入排序的时间复杂性。然而,我在第6行得到了上面提到的错误。在其他{int(输入)}期间也会发生此错误。任何帮助都会很好,谢谢。我的代码是: import random, matplotlib.pyplot as plt def input(): arr=[] ret_size=[] ret_count=[] n=int(input('enter the number of trials')) for i in ran
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():
及
在第一个函数中,您使用自己的同名函数重新定义了内置函数input()
(它接受0
或1
参数)(它只接受的0
参数,即无)。
因此,在第二个函数中,您调用的不是内置函数input()
,而是您自己的函数,当您使用1
参数(“输入试验次数”
)调用它时,您得到了一个相关错误
选择另一个名称来定义
input()
函数,然后也使用该名称来调用它。input
是一个内置函数。使用它作为自己函数的名称是一个非常糟糕的主意。调用自己的函数时,n=int(输入(“输入试验次数”)
将解析为什么?重命名函数将解决此问题。您定义了自己的input()
函数。您现在不能使用内置的input()
函数,除非做一些额外的工作。更容易重命名自己的函数。哦!谢谢你,我没注意到。等等,你怎么使用内置的阴影后呢@MartijnPieters i..呃..找朋友。@Paritossingh:3个选项:在绑定到input
之前创建一个别名(builtin\u input=input
),通过(import builtins
,…,builtins.input()
),或者通过删除隐藏内置的全局变量(del input
,这里不是选项)。
n=int(input('enter the number of trials'))