首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Python、列表、索引

Python、列表、索引
EN

Stack Overflow用户
提问于 2018-06-04 06:41:16
回答 1查看 536关注 0票数 -1

我正在尝试为我的python课程编写一个小程序(自学),有点像使用列表的字典。一个列表中有一个短语/单词,另一个列表中有该短语/单词的含义。通过用户输入,用户可以键入他们正在搜索的单词,并且将显示单词的含义。我在尝试获得要显示的含义时遇到了困难。我的代码如下:"aldo“是我的第一个输入(单词),"my name”是我的第二个输入(意思是)

代码语言:javascript
复制
word = []
meaning = []

user_word = input("Enter word: ")
user_meaning = input("Enter Meaning: ")
print(word)
print(meaning)

word = word + [user_word]
meaning = meaning + [user_meaning]

user_search = input("What word/phrase would you like to search: ")
search_index = word.index(user_search)
print(user_search + meaning.index(search_index))
EN

回答 1

Stack Overflow用户

发布于 2018-06-04 06:48:38

.index()查找索引,而不是值

你的意思是使用meaning[search_index]吗?

此外,要添加到列表中,使用.append(value)比使用+ [value]更可取

代码语言:javascript
复制
user_word = input("Enter word: ")
user_meaning = input("Enter Meaning: ")

word.append(user_word.strip())
meaning.append(user_meaning)

user_search = input("What word/phrase would you like to search: ")
search_index = word.index(user_search.strip())
print(user_search + " " + meaning[search_index])
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50671627

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档