如何使用字典在Python中将荷兰语翻译成英语

2024-10-05 10:40:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用这个代码翻译我是杰森,但当我使用它时,它不会打印出来任何东西,除非我像hello这样做一个字,然后它打印出guten标签,但就是这样。另外,我不能把输入数据转换成小写,以便能够将其与字典进行比较。我能做什么来使代码工作?

name = input("Please enter your name\n")
data = [input("Please enter sentence\n")]
data = data.lower() #to make sentence lowercase to be able to compare it to the words in the dictionary
dictionary = {"hello": "hallo", "i" : "ik", "am" : "ben"}

dictionary[name] = name
for word in data:
    if word in dictionary:
        print (dictionary[word],)

这是回溯

*请输入您的姓名

杰森

请输入句子

我是杰森*

^{pr2}$

Tags: theto代码nameinhelloinputdata
1条回答
网友
1楼 · 发布于 2024-10-05 10:40:46

你根据用户的回答列出了一个列表

In [26]: data = [input("Please enter sentence\n")]

Please enter sentence
You are Jason

In [27]: data
Out[27]: ['You are Jason']

列表没有lower方法。
首先获取用户的响应并将其转换为小写。在

^{pr2}$

然后在空白处split the string获取单个单词的列表。在

In [32]: data = data.split()

In [33]: data
Out[33]: ['you', 'are', 'jason']

相关问题 更多 >

    热门问题