我最初的代码是Python2.7.8中的一个基本计算器,但现在我决定加入一个用户可以输入多个值的函数。在
一旦输入了这些值,函数就可以运行,用户刚刚输入的值存储在一个变量中(函数参数),然后被告知他们是否输入了重复的值,这些值在逗号处被拆分,并被插入到一个列表中,然后函数运行。在
我已经创建了一个函数,它已经接受了与用户输入的AlgorithmListEntry相等的变量,当用户键入算法时会发生这种情况。在def findDuplicates(AlgorithmListEntry):
for i in len(range(AlgorithmListEntry)):
for j in len(1,range(AlgorithmListEntry)):
if AlgorithmListEntry[i]==AlgorithmListEntry[j]:
return True
return False
其中,函数也会查找参数的范围,但由于另一个错误,此操作不起作用
^{pr2}$
我现在收到一个错误for i in len(AlgorithmListEntry):TypeError: 'int' object is not iterable
为了便于查看,我只插入了与我的问题相关的代码部分i = True #starts outer while loop
j = True #inner scientific calculations loop
def findDuplicates(AlgorithmListEntry):
for i in len(AlgorithmListEntry):
for j in len(1(AlgorithmListEntry)):
if AlgorithmListEntry[i]==AlgorithmListEntry[j]:
return True
return False
while i==True:
UserInput=raw_input('Please enter the action you want to do: add/sub or Type algorithm for something special: ')
scienceCalc=('Or type other to see scientific calculations')
if UserInput=='algorithm':
listEntry=raw_input('Please enter numbers to go in a list:').split(",")
AlgorithmListEntry = list(listEntry)
print AlgorithmListEntry
print "The program will now try to find duplicate values within the values given"
findDuplicates(AlgorithmListEntry)
#i = False
问题为什么我会收到这两个错误?
如何在程序中成功实现此功能?这样用户就可以收到关于他们输入的值是否包含重复值的反馈?