Raw_input&如果声明

时间:2016-08-26 01:01:31

标签: python-2.7 variables if-statement dictionary raw-input

我正在尝试为名为" Sansa"的python做个人助理。 我是一个菜鸟,我希望我的代码只有在用户raw_input为yes时才执行我的initial_request变量。如果不是,我希望代码执行最后一个engine.say命令。我试图用字典来实现这个目的。谢谢,对不起凌乱的代码。

import webbrowser
import pyttsx

#adding voice (i will change)
engine = pyttsx.init()
engine = pyttsx.init()
voices = engine.getProperty('voices')
for voice in voices:
   engine.setProperty('voice', voice.id)
#Introducing Sansa and trying to navigate through the codes. 
#serviceP =input
first_answer = {'pos' : 'Yes', 'neg' : 'No'}
response = engine.say("Hello my Lord. My name is Sansa and I am your personal assistant. I am here to serve you. Can I be of service?")
engine.runAndWait()
st = raw_input("Hello my Lord. My name is Sansa and I am your personal assistant. I am here to serve you. Can I be of service?")
initial_Request =('What would you like to do? I can visit various websites, perform a google or youtube search, and so much more.')

#evaluate yes or no to execute initial_Request
if st == first_answer['pos']:
engine.say(initial_Request)
engine.runAndWait()

elif st == first_answer['neg']:
    engine.say("Ok. I'm here if you need me.")
    engine.runAndWait()

1 个答案:

答案 0 :(得分:0)

好的,我发现问题是让程序运行代码,例如添加变量unknownResponse,在engine.say()函数下添加print语句等。但是我仍然在处理一些错误,例如声音在if和elif语句中没有正确地继续。该计划正在聚集在一起。这是更新的代码。

import webbrowser
import pyttsx

#adding voice (i will change)
engine = pyttsx.init()
engine = pyttsx.init()
voices = engine.getProperty('voices')
for voice in voices:
   engine.setProperty('voice', voice.id)
   #Introducing Sansa and trying to navigate through the codes. Might need some type of loop to redirect the user

first_answer = {'pos' : 'Yes', 'neg' : 'No'}
engine.say("Hello my Lord. My name is Sansa and I am your personal   assistant. I am here to serve you. Can I be of service?")
engine.runAndWait()
st = raw_input("Hello my Lord. My name is Sansa and I am your personal   assistant. I am here to serve you. Can I be of service?")
initial_Request =("What would you like to do? I can visit various websites, perform a google or youtube search, and so much more.")
negative_Response =("Okay Master. I'm here if you change your mind.")
#evaluate yes or no to execute initial_Request
if st == first_answer['pos']:
    engine.say("What would you like to do? I can visit various websites, perform a google or youtube search, and so much more.")
    engine.runAndWait()
    print initial_Request

elif st == first_answer['neg']:
    engine.say("Okay Master. I'm here if you change your mind.")
    engine.runAndWait()
    print negative_Response
#add loop to make questions repeat

unknownResponse = True
while unknownResponse:
    againTry =raw_input("Sorry I don't understand. Please try again.")
    if st != first_answer['pos', 'neg']:
        engine.say("Sorry I don't understand. Please try again.")
        engine.runAndWait()
    unknownResponse = False