Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中搜索重复列表_Python - Fatal编程技术网

在Python中搜索重复列表

在Python中搜索重复列表,python,Python,输出 newWord = print(input('Enter a word: ')) myList = ["apple", "mango"] def ifWordAlreadyExists(str): for x in myList: if x == str: index = index(x) query = print(input('Word ', x ,'is already

输出

newWord = print(input('Enter a word: '))
myList = ["apple", "mango"]

def ifWordAlreadyExists(str):
    for x in myList:
        if x == str:
            index = index(x)
            query = print(input('Word ', x ,'is already in index ', index , 'are you sure you want to include it? '))
            if query == 'y':
                return str
            else:
                return 0
myList.append(ifWordAlreadyExists(newWord))
print(myList)
Enter a word: apple
apple
['apple', 'mango', None]
为什么查询没有显示,而newWord没有附加到myList? 另外,我是Python初学者,如果有人能指出我做错了什么,我将不胜感激:)

基本上
print()
返回
None
,您不需要将
input()
方法放在
print()
中,因为
input()
也可以在屏幕上打印内容,但可以接受输入。 您可以将代码更改为:

myList=[“苹果”、“芒果”]
newWord=input('输入一个单词:')
check=myList中的新词
如果没有,请检查:
myList.append(newWord)
其他:
print('Word',newWord,'已在列表中,是否确实要包括它?',end=“”)
query=input().lower().startswith('y')
如果查询:
myList.append(newWord)
打印(myList)
输出

newWord = print(input('Enter a word: '))
myList = ["apple", "mango"]

def ifWordAlreadyExists(str):
    for x in myList:
        if x == str:
            index = index(x)
            query = print(input('Word ', x ,'is already in index ', index , 'are you sure you want to include it? '))
            if query == 'y':
                return str
            else:
                return 0
myList.append(ifWordAlreadyExists(newWord))
print(myList)
Enter a word: apple
apple
['apple', 'mango', None]

打印功能不返回任何值

如果要打印输入的值,必须在底线上使用打印

Enter a word: apple
Word apple is already on the list are you sure you want to include it? yes
['apple', 'mango', 'apple']

newWord
None
如果您使用
newWord=print(输入('Enter a word:'))
,它应该是
newWord=input('Enter a word:')
。此外,您不必在函数内部使用for循环,例如,python列表的方法
index
可以直接解决它

newWord = input("Word:")
print(newWord) #like this
myList = ["apple", "mango"]

def ifWordAlreadyExists(str):
    for x in myList:
        if x == str:
            index = myList.index(x)
            query = input("Word {0} is already in index {1} are you sure you want to include it:".format(str,index))
            if query == 'y':
                return str
            else:
                return 0
myList.append(ifWordAlreadyExists(newWord))
print(myList)

您应该从str更改变量名,因为这实际上是字符串函数。例如,
str(1)
返回字符串
“1”
您希望
x==str
做什么?还有什么是
索引
?它没有定义,您正在调用它。函数正在追加
None
,因为没有使用
return
语句reached@Countour-积分我想将newWord与x进行比较,如果它们相等,则显示queryprint return None type如果您的条件不对,您应该立即添加单词,如果
newWord不在我的列表中
谢谢!现在已经纠正了。