在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不在我的列表中
谢谢!现在已经纠正了。