Search code examples
pythonloopsbreakcontinue

loops,dont understand


I am begginer in python and I dont understand,have to meake a loop for this comands,I wont to they work in loop,forexample - when I told "write file" code works, and do the next step of code , "read file" and then exit. But now work only one part of code! Thank You a lot for yor time!

import pyttsx3
engine = pyttsx3.init()
import speech_recognition as sr
import datetime
import webbrowser
import speech_recognition as sr
import pytesseract
from PIL import Image
from pytesseract import image_to_string
pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'




def say(talk):
    engine.say(talk)
    engine.runAndWait()

say("Hello, Let's Start!")


record = sr.Recognizer()
try:

    with sr.Microphone(device_index=1) as source:
        print("Speak Now Please!........")
        audio = record.listen(source)
        result = record.recognize_google(audio)
        result = result.lower()
        print(result)


        if result == "write file":
            str_file = image_to_string(Image.open('Images/photo.png'))
            file = open("text_from.txt", "w")
            file.write(str_file)
            file.close()



        elif result == "read file":
            file = open("text_from.txt", "rt")
            for line in file:
                print(line)
            file.close()

        elif result == "exit":
            print("exit")
            



except sr.UnknownValueError:
    print("Talk again")

except sr.RequestError:
    print("Somthing going wrong")


Solution

  • I dont know how correct it is, but i find the way.maybe thats helps someone.

    `import pyttsx3
    engine = pyttsx3.init()
    import speech_recognition as sr
    import datetime
    import webbrowser
    import speech_recognition as sr
    import pytesseract
    from PIL import Image
    from pytesseract import image_to_string
    pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'
    
    
    
    
    def say(talk):
        engine.say(talk)
        engine.runAndWait()
    
    say("Hello, Let's Start!")
    
    result = False
    record = sr.Recognizer()
    try:
        with sr.Microphone(device_index=1) as source:
            print("SPEAK NOW ...................")
            audio = record.listen(source)
            result = record.recognize_google(audio)
            result = result.lower()
            print(result)
    
    
            while result == "write file":
                str_file = image_to_string(Image.open('Images/photo.png'))
                file = open("text_from.txt", "w")
                file.write(str_file)
                file.close()
    
                with sr.Microphone(device_index=1) as source:
                    print("SPEAK NOW ...................")
                    audio = record.listen(source)
                    result = record.recognize_google(audio)
                    result = result.lower()
                    print(result)
    
    
    
            while result == "read file":
                 file = open("text_from.txt", "rt")
                 for line in file:
                     print(line)
                 file.close()
    
                 with sr.Microphone(device_index=1) as source:
                     print("SPEAK NOW ...................")
                     audio = record.listen(source)
                     result = record.recognize_google(audio)
                     result = result.lower()
                     print(result)
    
    
            while result == "exit":
                print("")
                break
    
    
    
    
    
    except sr.UnknownValueError:
        print("Talk again")
    
    except sr.RequestError:
        print("Somthing going wrong")
    `