我正在尝试使用此代码来翻译我是Jason,但是当我使用它时,它什么都不会打印出来,除非我打个招呼,除非它打出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],)
这是回溯
*请输入您的姓名
Jason
请输入句子
我是Jason *
Traceback (most recent call last):
File "C:/Users...", line 3, in <module>
data = data.lower()
AttributeError: 'list' object has no attribute 'lower'
答案 0 :(得分:0)
您从用户的回复中列出了一个列表
In [26]: data = [input("Please enter sentence\n")]
Please enter sentence
You are Jason
In [27]: data
Out[27]: ['You are Jason']
列表没有lower
方法。
首先获取用户的响应并将其转换为小写。
In [28]: data = input("Please enter sentence\n")
Please enter sentence
You are Jason
In [29]: data
Out[29]: 'You are Jason'
In [30]: data = data.lower()
In [31]: data
Out[31]: 'you are jason'
然后在空白处split the string以获得单个单词的列表。
In [32]: data = data.split()
In [33]: data
Out[33]: ['you', 'are', 'jason']