-3

Just started learning programming and started dabbling with simple chatbot code. I've got responses in text, however I want my computer (Mac) to speak the response AND ONLY the response at the same time as it appears. Here's the code:

import random 

import sys

import os

def show_response(options):

choice = random.randint(0, len(options) -1)

print(options[choice])

random.seed(None)

while True:
    userInput = input(">>>")

    if userInput in ["Ava, you there?"]:
        list = ["Indeed I am, Sir.", "Yes, Sir. What do you need?", "For you, Sir, always.", "I am now.", "What is it, Sir?", "I'm right here.", "How may I assist you, Sir?", "How may I assist you?", "You need something, Sir?"]
        choice = random.randint(0, len(list) -1)
        print(list[choice])
    elif userInput in ["Wake up, Ava"]:
        list = ["What is it, Sir?", "How can I help?", "How can I help, Sir?", "Hello, Sir.", "Hello, Sir. Is there anything you need?", "How may I assist you?", "How may I assist you, Sir?"]
        choice = random.randint(0, len(list) -1)
        print(list[choice])
    elif userInput in ["Hey, Ava"]:
        list = ["What is it, Sir?", "How can I help?", "How can I help, Sir?", "Hello, Sir.", "Hello, Sir. Need anything?", "How may I assist you?", "How may I assist you, Sir?", "You need something, Sir?"]
        choice = random.randint(0, len(list) -1)
        print(list[choice])

If this is a repeat I apologies. I'm more in need of fitting the solution within this structure though just to help me learn on where it's placed and how to use it.

NOTE: This isn't a dupe of the question marked. Although the question this is supposedly duping does allow you to turn text to speech, it does not deal with turning listed random responses into speech, which is what I needed to do. It did however allow me to get the speech working though so thank you. I've found the answer elsewhere and placed it below.

Stewie
  • 105
  • 2
  • To format code, select the block and press the button in the toolbar that looks like curly braces. It works perfectly. You don't need the extra blank lines between every one of your lines of code. – Cody Gray - on strike Jan 10 '16 at 13:47
  • `I'm more in need of fitting the solution within this structure` is a fancy way of saying "do it for me". It sounds like you found what you needed, but didn't want to take the time needed to learn it well enough to use it. – takendarkk Jan 10 '16 at 13:48
  • Possible duplicate of [How to make Python speak](http://stackoverflow.com/questions/1614059/how-to-make-python-speak) – takendarkk Jan 10 '16 at 13:49
  • @takendarkk No, this is more than likely going to be set as a duped question. The problem with that is, the question that already has the answer may be used for a different structure, one that I don't understand. I need to know how to place the specific code in THIS specific structure for me to understand it. – Stewie Jan 10 '16 at 13:51
  • Well, did you try to put the other code into your structure yet? If you did, you didn't show us. If you didn't, that should be step 1. – takendarkk Jan 10 '16 at 13:52
  • No because I don't know how. You seem to have skipped passed the "I'm just starting to learn" part of my question. A prime indication that I have NO idea on how to get it working. I've only tapped on 'if's, 'elif's and 'else's in text responses. No clue how to tap into the computers speech systems – Stewie Jan 10 '16 at 13:54

1 Answers1

0

As I got no responses on here to help the situation I went else where and found out the answer to this SPECIFIC question. Although the question this was supposedly duping did allow Python to speak, it did not set the speech to the responses with in the list. It did however allow me to get the speech working though so thank you.

I simply added this code below EVERY 'print(list[choice])' including 'print(options[choice])' where it declares the functions.

The needed code: os.system("say " + "'" + list[choice] + "'")

This will then allow it to speak the randomly chosen responses. However, for some reason it doesn't seem to like apostrophes so make sure that all the responses in the list have none! Commas, points, question marks, etc appear to be fine, but apostrophes are a no go!

This worked for me, not sure if it will work for other iterations of code but I hope that it does.

Stewie
  • 105
  • 2