Python 我不明白
我是python的begginer,我不明白,我必须为这个命令做一个循环,我不会让他们在循环中工作,例如,当我告诉“写文件”代码工作时,然后执行代码的下一步,“读文件”,然后退出。但是现在只工作代码的一部分! 非常感谢您抽出时间Python 我不明白,python,loops,break,continue,Python,Loops,Break,Continue,我是python的begginer,我不明白,我必须为这个命令做一个循环,我不会让他们在循环中工作,例如,当我告诉“写文件”代码工作时,然后执行代码的下一步,“读文件”,然后退出。但是现在只工作代码的一部分! 非常感谢您抽出时间 import pyttsx3 engine = pyttsx3.init() import speech_recognition as sr import datetime import webbrowser import speech_recognition as s
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")
我不知道这有多正确,但我找到了方法。也许这对某人有帮助
`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")
`