python - 供用户在 Python 中选择输入模式的函数
问题描述
我正在尝试在 python 中做一个助手类的东西,所以基本上我想要的是它要求用户在两种输入模式之间进行选择,即文本或语音输入。
但问题是我无法将它实现到主代码中。以下是我尝试制作的示例代码:
import keyboard
import speech_recognition as sr
def takecommandti():
print(" ")
rt=input(">>")
return rt.lower()
def takeCommandsi():
print("Listening....")
r = sr.Recognizer()
r.dynamic_energy_threshold=False
r.energy_threshold=4000
r.pause_threshold = 1
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
said=""
try:
print("Recognizing....")
said = r.recognize_google(audio,language='en-in')
print(f"You Said : {said}\n")
except sr.UnknownValueError :
print("could not understand audio \n ~Trying Again~")
return takeCommandsi()
except sr.RequestError as e:
print("Could not request results, check your internet connection; {0}".format(e))
return "None"
return said.lower()
input_mode_selection_variable = 0
print("Please Select in which input Mode you want to Use :) ")
print("1.Press 't' for Text Input Mode \n2.Press 's' for Speech Input Mode ")
while True :
if keyboard.is_pressed('t'):
print('You have successfully Selected : Text Input Mode')
input_mode_selection_variable=False
break
elif keyboard.is_pressed('s'):
print('You have successfully Selected : Speech Input Mode')
input_mode_selection_variable=True
break
def takecommand():
if input_mode_selection_variable == False:
takecommandti()
elif input_mode_selection_variable ==True:
takeCommandsi()
我怎样才能做到这一点?
解决方案
当您在其他脚本中使用 takecommand() 函数时,我认为 input_mode_selection_variable 不会起作用。
推荐阅读
- regex - Azure AD B2C 自定义密码重置策略不会使用 + char\sign 验证电子邮件
- windows - 在 Windows 命令提示符下着色 Perl 输出
- r - 在 R 中使用 64 hz 进行上采样
- ruby-on-rails - 谷歌云上的 Rails Cron
- java - HADOOP - 无法初始化 MapOutputCollector org.apache.hadoop.mapred.MapTask$MapOutputBuffer java.lang.ClassCastException:类 java.lang.Double
- mysql - 连接到 mysql 5.6 db 的 MySQL Workbench 的正确版本是什么?
- docker - Nginx 反向代理无法通过主机名访问 docker 容器
- python - 在 Python 中删除字符串中无关空格并在单词之间保留 1 个空格的代码
- javascript - 图片名称存储在本地存储时在reactjs中显示图片
- mysql - 无法连接到安装在另一个虚拟机上或通过网络的 MySQL