2016-08-26 3 views
0

J'essaie de faire un assistant personnel pour python nommé "Sansa". Je suis un noob et je veux que mon code n'exécute ma variable initial_request que si les utilisateurs raw_input est oui. Si c'est non, je veux que le code exécute la dernière commande engine.say. J'ai essayé d'y parvenir en utilisant un dictionnaire. Merci et désolé pour le code désordonné.Raw_input & If Statements

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() 
+0

Je pense que vous avez juste une erreur d'indentation pour les deux lignes après la ligne 'if st == first_answer ['pos']:'. Comme il n'est pas indenté, il ne figure pas dans l'instruction if et s'exécutera à chaque fois – mitoRibo

+0

Cela ne semble pas le résoudre. Lorsque je cours le code si je réponds oui ou non au raw_input, il n'exécute pas l'instruction if ou l'instruction elif. Ceci est la sortie –

+0

Python 2.7.12 (par défaut 1er Jul 2016, 15:12:24) [GCC 5.4.0 20160609] sur linux2 type "copyright", pour plus d'informations "crédits" ou "licence()" . >>> =============== RESTART: /home/master/Desktop/Sansa_/Sansa.py =============== Bonjour mon Seigneur. Je m'appelle Sansa et je suis votre assistant personnel. Je suis ici pour vous servir. Puis-je être de service oui >>> =============== RESTART: /home/master/Desktop/Sansa_/Sansa.py ========= ====== Bonjour mon Seigneur. Je m'appelle Sansa et je suis votre assistant personnel. Je suis ici pour vous servir. Puis-je être utile? Non >>> –

Répondre

0

D'accord, j'ai trouvé le problème pour rendre le programme géré par le code tel que l'ajout de la unknownResponse variable ajoutant des instructions d'impression sous les fonctions engine.say(), etc. Cependant, je suis encore en train de travailler sur quelques bugs tels que la voix ne continue pas correctement à travers les déclarations if et elif. Le programme se réunit cependant. Voici le code mis à jour.

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