如何修复错误:TypeError:'set'对象不可调用

时间:2019-07-26 02:07:42

标签: python python-3.x

我写了一个代码将英语单词翻译成葡萄牙语。输出葡萄牙语单词时,我希望程序播放输出葡萄牙语单词的发音。播放发音的代码没有输出我的期望。使用打印功能翻译后,如何为每个单词播放单词发音?

这是与我正在学习的python语言类相关的练习。我用英语列出了英语单词及其翻译的葡萄牙语,并记录了每个葡萄牙语单词的发音,并保存为.mp3文件。我编写了开发迷你词典的代码,但它确实按预期工作。

    vocabulary = {"forest":"floresta","earth":"terra","rain":"chuva"}
    pronunciation = {"floresta", "terra", "chuva"}

    def translate_to_portuguese(word):

        return vocabulary[word]

    word = input("Enter an English word: ")

    print(translate_to_portuguese(word.lower()))

    from pygame import mixer

    mixer.init()

    if word in pronunciation(): 

        mixer.music.load(word .mp3)

    mixer.music.play()

我希望英语单词能被翻译成葡萄牙语,并且葡萄牙语的发音会发挥出来。但是,这是输出:

Enter an English word: forest
floresta
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "C:\Users\Gerson Maso\Desktop\Python Training\Gerson Exercises\Dictionary code\sound test.py", line 16, in <module>
    if word in pronunciation():
TypeError: 'set' object is not callable

可以帮忙吗?

1 个答案:

答案 0 :(得分:-1)

尝试更改

if word in pronunciation(): 
    mixer.music.load(word .mp3)

if word in pronunciation: 
    mixer.music.load(word + ".mp3")