在字典中查找值?

发布于 2024-12-27 12:19:26 字数 556 浏览 1 评论 0原文

我正在尝试打印一条消息。 如果在字典中找不到某个单词,那么它应该打印出一条消息而不是给出错误。 我的想法是,

if bool(bool(dictionary[word])) == True:
    return dictionary[word]
else:
    print 'wrong'

但当我写一些字典中没有的内容时,它不起作用,而是给出类似的内容

Traceback (most recent call last):
  File "<pyshell#34>", line 1, in <module>
    translate_word('hous')
  File "H:\IND104\final\Project 4 - Italian-Spanish Translator\trial 1.py", line 21, in translate_word
    if bool(bool(dictionary[word])) == True:
KeyError: 'hous' 

那么我怎样才能打印出错误消息,谢谢。

I am trying to print out a message.
If a word in dictionary is not found then it should print out a message instead of giving an error.
What I thought is

if bool(bool(dictionary[word])) == True:
    return dictionary[word]
else:
    print 'wrong'

but it does not work when I write something that is not in dictionary instead it gives something like this

Traceback (most recent call last):
  File "<pyshell#34>", line 1, in <module>
    translate_word('hous')
  File "H:\IND104\final\Project 4 - Italian-Spanish Translator\trial 1.py", line 21, in translate_word
    if bool(bool(dictionary[word])) == True:
KeyError: 'hous' 

So how can I print out an error message thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

乙白 2025-01-03 12:19:26

您需要使用 in 运算符来测试某个键是否在字典中。使用您的变量名称,这将变为:

if word in dictionary:

如果您希望一次性检查键是否存在并检索值,您可以使用 get() 方法:

value = dictionary.get(word)
if value is not None:
    ...

您可以向 get() 提供自己的默认值,如果 key没有找到。然后你可以像这样编写你的代码:

print dictionary.get(word, 'wrong')

You need to use the in operator to test whether or not a key is in the dictionary. With your variable names this becomes:

if word in dictionary:

If you wish to check for the presence of the key and retrieve the value in one go you can use the get() method:

value = dictionary.get(word)
if value is not None:
    ...

You can supply your own default value to get() which is returned if the key is not found. Then you could write your code like this:

print dictionary.get(word, 'wrong')
冷情妓 2025-01-03 12:19:26

实现您想要的效果的一种方法是使用 try/except 块:

try:
    return dictionary[word]
except KeyError:
    print 'wrong'
    return None

One way to achieve what you want is a try/except block:

try:
    return dictionary[word]
except KeyError:
    print 'wrong'
    return None
一花一树开 2025-01-03 12:19:26
if word in dictionary:
    return dictionary[word]
else:
    print("wrong")
if word in dictionary:
    return dictionary[word]
else:
    print("wrong")
笑脸一如从前 2025-01-03 12:19:26

索引字典假设键已经存在,为了测试键是否在字典中,请尝试以下操作:

#Check to see if a key is in a dictionary
dictionary = {'key' : 123}
if 'key' in dictionary:
 #Do something
else:
 #Do something else

[dictionary] 中的 [key] 现在是比之前的 has_key([key]) 字典方法更受欢迎的语法:参考

Indexing a dictionary assumes that the key is already there, in order to test if the key is in the dictionary, try this:

#Check to see if a key is in a dictionary
dictionary = {'key' : 123}
if 'key' in dictionary:
 #Do something
else:
 #Do something else

The [key] in [dictionary] is now the favoured syntax over the previous has_key([key]) dictionary method: reference

笔芯 2025-01-03 12:19:26

是时候学习一些异常处理了:

try:
    some_stuff()
except:
    print "Sorry, an error occured"

Time to learn some exception handling:

try:
    some_stuff()
except:
    print "Sorry, an error occured"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文