def emotional():
words = emotion.split(' ')
art = {
":(": "/伤心",
":)": "/开心",
":|": "/还好",
}
output=" "
for ch in words:
output +=art.get(ch,ch) + " "
return(output)
emotion = input("how do you felling?: ")
print(emotional())
这个代码是我练习,感觉思路有点绕不过来写的。解释一下:
首先:
def emotional(): #这个是定义一个函数 words = emotion.split(' ') #split是将输入的一段话分开,给words art = { #字典里面一个键值对应着一个信息 ":(": "/伤心", ":)": "/开心", ":|": "/还好", } output=" " #设置输出的内容,就是先定义 for ch in words: #words就是输入的内容,遍历有没有字典里面的键值比如:":(" output +=art.get(ch,ch) + " " #用.get函数从words获取字典键值对应的信息输出output return(output) emotion = input("how do you felling?: ") #输入信息,传递给word,也就是emotion.split(' ') print(emotional()) #打印函数的return