Home > other >  how do I fix this python?
how do I fix this python?

Time:12-08

When I run my program I get this following error How do I fix this?

I need this python script to convert my mp3 file to text file to do a project on Voice Cloning. It has an error, but I cannot find it:

  File "convert.py", line 13
    no = [no, nah, nope, n, not]
                               ^
SyntaxError: invalid syntax

Here is the Code for my program :

import speech_recognition as sr

def main():
    sound = "file.mp3"
    r = sr.Recognizer()

    with sr.AudioFile(sound) as source:
        r.adjust_for_ambient_noise(source)
        print("Converting Audio File to Text...")
        audio = r.listen(source)

        yes = [yes, ye, yo, yah, y, yup]
        no = [no, nah, nope, n, not]

        choice = input().lower()
        if choice is yes:
           try:
               print("Converted Audio As : \n "   r.recognize_google(audio))

           except Exception as i:
               with open('output.txt', 'w') as f:
                   f.write("balance %d" % balance")
        elif choice is no:
           try:
               print("Converted Audio As : \n "   r.recgnonize_google(audio))

           except Exception as j:
               print(j)
        else:
           sys.stdout.write("Please Enter (Y/N) to Continue")

if __name__ == "__main__":
    main()

CodePudding user response:

  1. not is a reserved keyword in Python, so it is a SyntaxError to use it in place of an identifier.

  2. Also, your list elements should be quoted, otherwise they are not strings.

  3. In addition to that, you have an extra quote here:

f.write("balance %d" % balance")
  1. Note that this if choice is yes does not do what you think it does. You need to use in to check if an element is in a container.

CodePudding user response:

Add quotes around the items in your lists so they are recognized as strings.

yes = ['yes', 'ye', 'yo', 'yah', 'y', 'yup']
no = ['no', 'nah', 'nope', 'n', 'not']

not is also a keyword in the syntax like 'if, while, for' etc. So also bare that in mind.

CodePudding user response:

if you want to store strings you have to use single quotes ' double quotes " or three double quotes """, so:

    yes = ["yes", "ye", "yo", "yah", "y", "yup"]
    no = ["no", "nah", "nope", "n", "not"]

but if they are variables, and that's not clear, you don't need that, but again not is a reserved keywoard and that will raise and exception, you can't use it as a variable name, maybe use not_ or NOT

  • Related