Python 为什么检查一个单词是否是字典中的关键字不起作用?

Python 为什么检查一个单词是否是字典中的关键字不起作用?,python,dictionary,Python,Dictionary,我试图检查用户给定的单词是否是我字典中的关键字,但响应总是转到“else”部分 这是我的dic: mydic = {'Paris':[132,34] 'Rome':[42,35] 'San Remo':[23,66]} 代码如下: my_input = input('Write a command').lower() useful_input = my_input.split() if my_input == 'info': print("Write 'city'

我试图检查用户给定的单词是否是我字典中的关键字,但响应总是转到“else”部分

这是我的dic:

mydic = {'Paris':[132,34] 'Rome':[42,35] 'San Remo':[23,66]}
代码如下:

my_input = input('Write a command').lower()
useful_input = my_input.split()


if my_input == 'info':
        print("Write 'city' and city name to get the info")
            
elif their_command == 'city' and phrase:
    if phrase in mydic.keys():
        print(f"{phrase} city has {mydic.get(phrase)[0]} churches.")
    else:
        print('Wrong')

因此,我需要搜索单词“city”后的第二个单词(或第二个和第三个单词),即命令,是否是我字典中的关键字。如果是,我需要返回print语句中键的第一个值。对于我的代码,它直接转到“else”并打印“error”,那么为什么会发生这种情况呢?

您将输入转换为小写

你的钥匙大小不一


顺便说一下,您可以在mydic中编写
短语
,您不需要获取密钥列表。你可以在字典里查到
mydic[短语]
。该方法与get的唯一区别在于处理不存在的条目。

将输入转换为小写

你的钥匙大小不一


顺便说一下,您可以在mydic中编写
短语
,您不需要获取密钥列表。你可以在字典里查到
mydic[短语]
。该方法与get的唯一区别在于处理不存在的条目。

我认为是因为您的输入是小写的。请尝试下面的代码

mydic = {'Paris':[132,34], 'Rome':[42,35], 'San Remo':[23,66]}

my_input = input('Write a command: ')
useful_input = my_input.split()
their_command = useful_input[0]
words = useful_input[1:]
phrase = " ".join(words)

if my_input.lower() == 'info':
        print("Write 'city' and city name to get the info")
            
elif their_command.lower() == 'city' and phrase:
    if phrase in mydic.keys():
        print(f"{phrase} city has {mydic.get(phrase)[0]} churches.")
    else:
        print('Wrong')

我想是因为你的输入是小写的。请尝试下面的代码

mydic = {'Paris':[132,34], 'Rome':[42,35], 'San Remo':[23,66]}

my_input = input('Write a command: ')
useful_input = my_input.split()
their_command = useful_input[0]
words = useful_input[1:]
phrase = " ".join(words)

if my_input.lower() == 'info':
        print("Write 'city' and city name to get the info")
            
elif their_command.lower() == 'city' and phrase:
    if phrase in mydic.keys():
        print(f"{phrase} city has {mydic.get(phrase)[0]} churches.")
    else:
        print('Wrong')

短语=第二个和第三个参数连接(来自代码短语的第一个示例=“”.join(words)),所以如果您在输入中键入'city Paris Rome'

在线

if phrase in mydic.keys():
Python将比较dictionary的键值中的parisrome是否为false,至少在mydic中有parisrome city

我的建议不是连接字符串,而是使用有用的输入列表中的项迭代抛出字典


令人惊讶的例子,你可以在这里找到短语=第二个和第三个参数连接(来自代码短语的第一个示例=''.join(words)),所以如果你在输入中键入'city Paris Rome'

在线

if phrase in mydic.keys():
Python将比较dictionary的键值中的parisrome是否为false,至少在mydic中有parisrome city

我的建议不是连接字符串,而是使用有用的输入列表中的项迭代抛出字典


令人惊奇的例子,你可以在这里找到字典

你强制输入的单词小写-也许你应该对你的字典键做同样的操作。如果您引入了一个临时的
print(phrase)
语句,您可能已经发现了这个问题。您将输入的单词强制为小写-也许您应该对字典键执行相同的操作。如果您引入了一个临时的
打印(短语)
语句,您可能已经发现了这个问题。谢谢您的回答。我添加了短语.title(),但仍然得到相同的结果。@satoru字符串是不可变的-
phrase.title()
不会更改
phrase
,您需要
phrase=phrase.title()
@Joffan非常感谢,您是对的,解决了这个问题。谢谢您的回答。我添加了短语.title(),但仍然得到相同的结果。@satoru字符串是不可变的-
phrase.title()
不会更改
phrase
,您需要
phrase=phrase.title()
@Joffan非常感谢,您是对的,这就解决了问题。