1
Contributor
1
Reply
15
Views
8 Months
Discussion Span
Last Post by Arpita_1
0
import random
import string

def printAll(conversation):
   for line in conversation:
       print (line)

def main():
   cannedlist = ["How are you?", "Why is the sky blue?" , "How's the
weather?" , "When will it end?" , "Why are we here?" , "Sounds good!",
"That's great!", "What time is it?"]
   num_canned = len(cannedlist)
   response = input("How many rounds of conversation?")
   conversation = []
   number = random.randrange(0,num_canned)
   computer = cannedlist[number]
   print (computer)
   conversation.append(computer)

   # perform specified number of interaction rounds
   for interaction in range(eval(response)):
       user = input("")
       conversation.append(user)

       # search for mirror words and construct response
       userwords = user.split()
       mirror_words_found = False
       computer = ""
       for word in userwords:
           if word == "I":
               new_word = "You"
               mirror_words_found = True;
           elif word == "you":
               new_word = "I"
               mirror_words_found = True;
           elif word == "You":
               new_word = "I"
               mirror_words_found = True;
           elif word == "am":
               new_word = "are"
               mirror_words_found = True;
           elif word == "are":
               new_word = "am"
               mirror_words_found = True;
           elif word == "me":
               new_word = "you"
               mirror_words_found = True;
           elif word == "you're":
               new_word = "I'm"
               mirror_words_found = True;
           elif word == "I'm":
               new_word = "You're"
               mirror_words_found = True;
           else:
               new_word = word
           computer = computer + new_word + " "
       computer = computer.replace(".","?");
       computer = computer.replace("!","?");

       # if we found no mirror words, use a canned response instead
       if not mirror_words_found:
           number = random.randrange(0,num_canned)
           computer = cannedlist[number]

       # use whatever response we came up with
       print (computer)
       conversation.append(computer)

   # end the conversation
   computer = ("Goodbye!")
   print (computer)
   conversation.append(computer)
   print()
   print()
   printAll(conversation)

main()
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.