Python - 数组的某些部分被忽略了?为什么?固定?

时间:2017-01-25 20:16:58

标签: python macos

我已经开始在python中创建一个AI,并使用os.system创建一个小的BASh命令(我正在使用Mac!)。 AI的命令类似于:

if uInput.upper() in uiArray:
            tmp = choice(aiArray)
            system("say -i " + str(tmp))

选择import ed from randomsay -i是内置于Mac shell中的命令,可以大声读取文本。它工作正常,直到我创建uiQuoteAsk数组。它主要起作用,但它忽略了 "PLEASE TELL ME QUOTE", "PLEASE TELL QUOTE""QUOTE""GIVE ME A QUOTE"部分。如果我运行我的AI,输入这四个短语时什么都不会发生。我该如何解决这个问题,为什么要这样做呢? 这是我的代码:

# coding: utf-8
# TANGIS-V, the Text-based Artificial Next-Gen Intelligence Sys with Voice
# Programmed by Keegan Kuhn
# 
# Update info:
# v0: Creation, 1.18.17
# v0.1: Main structure built, 1.18.17
# v0.1.1: Content added to user and AI greetings, 1.19.17
# v0.1.1.1: Added greeting to main function, 1.19.17
# v0.1.1.2: Added user input to main function, 1.19.17
# v0.1.2: Changed 'input' to 'raw_input' & added 1st AI reply, 1.20.17
# v0.1.3: Main functon into loop & added AI greeting before 'main()', 1.20.17
# v0.1.3.1: uiNameAsk array added, 1.20.17
# v0.1.3.1.1: Content added to uiNameAsk array, 1.20.17
# v0.2: 'system' now imports from 'os', 1.20.17
# v0.2.1: Added some more arrays and replies, 1.20.17
# v0.3: Added voice, 1.23.17
# v0.3.1: Added math mod, 10.23.17
# v0.3.1.1: Enables user to perform math functions, 1.25.17
# v0.3.1.2: Adds voice to beginning, 1.25,17

###################################################
#                  VVV  MAIN VVV                  #
###################################################

# Main
def main():
    tmp = "null"
    uInput = raw_input(">>> ")
    if uInput.upper() in uiGreetings:
        tmp = choice(aiGreetings)
        system("say -i " + str(tmp))
    #
    #
    #
    if uInput.upper() in uiNameAsk:
        tmp = choice(aiNameAnswer) 
        system("say -i " + str(tmp))
    #
    #
    #
    if uInput.upper() in uiClear:
        system("clear")
    #
    #
    #
    if uInput.upper() in uiJokeAsk:
        tmp = choice(aiJoke)
        system("say -i " + str(tmp))
    #
    #
    #
    if uInput.upper() in uiMath:
        system("say -i Read the text below for math help.")
        print "############################################"
        print "'+' for addition"
        print "'-' for subtraction"
        print "'*' for multiplication"
        print "'** for exponents"
        print "'/' for division"
        print "And '.' for decimals"
        print "Find sqrt by typing 'sqrt()' and putting the number in parenthesis."
        print "Type 'pi' or 'phi' in equation to easily get its value."
        print "############################################"
        print
        tmp = eval(raw_input("Type your math problem >>> "))
        system("say -i The answer is " + str(tmp) + ".")
        system("say -i Quitting math interface.")
    #
    #
    #
    if uInput.upper() in uiQuoteAsk:
        tmp = choice(aiQuote)
                system("say -i " + str(tmp))
    #
    #
    #
    main()

###################################################
#                  ^^^  MAIN ^^^                  #
###################################################



###################################################
###################################################



###################################################
#                  VVV  INIT VVV                  #
###################################################

# Init
print "[ ] Starting TANGIS ..."
#
#
# Init, mod import
from random import choice
from os import system
from math import sqrt
#
#
# Init, array setup
#
# Init, user-input array setup 
uiGreetings = [
    "HI",
    "HEY",
    "HELLO",
    "HOWDY",
    "GOOD DAY",
    "GOOD MORNING",
    "MORNING",
    "GOOD AFTERNOON",
    "AFTERNOON",
    "GOOD EVENING",
    "GOOD EVE",
    "EVENING",
    "EVE",
    "YO",
    "WHATS UP",
    "WHATSUP",
    "SUP"
    ]

uiNameAsk = [
    "WHAT IS YOUR NAME",
    "WHATS YOUR NAME",
    "WHAT ARE YOU CALLED",
    "WHAT ARE YOU NAMED",
    "WHATRE YOU NAMED",
    "WHATRE YOU CALLED"
]

uiClear = [
    "CLEAR",
    "CLS",
    "CLEAR THE SCREEN",
    "CLEAR SCREEN",
    "CLEARSCREEN",
    "CLEAN",
    "CLEAN THE SCREEN",
    "CLEAN SCREEN",
    "CLEANSCREEN",
    "WIPE",
    "WIPE THE SCEEN",
    "WIPE SCREEN",
    "WIPESCREEN",
    "SCREEN CLEAR",
    "SCREENCLEAR",
    "SCREEN CLEAN",
    "SCREENCLEAN",
    "SCREEN WIPE",
    "SCREENWIPE"
]

uiJokeAsk = [
    "CAN YOU TELL ME A JOKE",
    "TELL ME A JOKE",
    "CAN YOU TELL JOKE",
    "TELL JOKE",
    "CAN YOU JOKE",
    "JOKE",
    "CAN YOU SAY A JOKE",
    "SAY A JOKE",
    "SAY SOMETHING FUNNY",
    "TELL ME SOMETHING FUNNY",
    "TELL SOMETHING FUNNY",
    "FUNNY" ,
    "ANOTHER JOKE",
    "TELL ME ANOTHER JOKE",
    "CAN TELL ME ANOTHER JOKE"
]

uiMath = [
    "MATH",
    "DO MATH",
    "CAN YOU DO MATH",
    "MATHEMATICS",
    "DO MATHEMATICS",
    "CAN YOU DO MATHEMATICS",
    "MATHEMATIC",
        "DO MATHEMATIC",
        "CAN YOU DO MATHEMATIC",
    "MATHS",
        "DO MATHS",
        "CAN YOU DO MATHS",
    "LETS DO MATH",
    "LETS DO SOME MATH"
]

uiQuoteAsk = [
    "CAN YOU TELL ME A QUOTE",
        "CAN YOU TELL ME QUOTE",
        "CAN YOU TELL QUOTE",
    "CAN YOU PLEASE TELL ME A QUOTE",
    "CAN YOU PLEASE TELL ME QUOTE",
    "CAN YOU PLEASE TELL QUOTE",
    "PLEASE TELL ME A QUOTE",
    "PLEASE TELL ME QUOTE"
        "PLEASE TELL QUOTE",
    "TELL A QUOTE",
    "CAN YOU TELL A QUOTE",
        "CAN YOU PLEASE TELL A QUOTE",
        "PLEASE TELL A QUOTE",
    "TELL ME A QUOTE"
    "QUOTE",
    "GIVE ME A QUOTE",
    "TELL ME A QUOTE"
]

#
#
# Init, AI array setup
aiGreetings = [
    "Greetings.",
    "Welcome.",
    "Hello.",
    "Good day."
    ]

aiNameAnswer = [
    "I am known as TANGIS-V.",
    "I am known as TANGIS-V, the Text-based Artificial Next-Gen Intelligence Sys with Voice.",
    "I am called TANGIS-V.",
    "I am TANGIS-V.",
    "I am TANGIS-V, the Text-based Artificial Next-Gen Intelligence Sys.",
    "I am known as TANGIS-V, the Text-based Artificial Next-Generation Intelligence System with Voice.",
    "I am TANGIS-V, the Text-based Artificial Next-Generation Intelligence System with Voice."
]

aiJoke = [

    'How did the programmer die in the shower? Because he read the shampoo bottle instructions: Lather. Rinse. Repeat.',

    'Two strings walk into a bar and sit down. The bartender says, “So what’ll it be?” The first string says, “I think I’ll have a beer quag fulk boorg jdk^CjfdLk jk3s d#f67howe%^U r89nvy~~owmc63^wmc63^Dz x.xvcu.” “Please excuse my friend,” the second string says, “He isn’t null-terminated.”'

]

aiQuote = [
    "Give a fish to a boy, give him a meal for a day. Teach a boy to fish, give him a meal for a lifetime. - Unknown",
    "Do not ask what your country can do for you, ask what you can do for your country. - John F. Kennedy"
]

#
#
# Init, misc vars
ver = "0.3.1"
tmp = 0
pi = 3.14
phi = 1.68

print "[*] TANGIS started !"

system("say -i " + str(choice(aiGreetings)) )

main()

###################################################
#                  ^^^  INIT ^^^                  #
###################################################

1 个答案:

答案 0 :(得分:1)

您的代码将两个字符串合并在一起,因为您缺少逗号:

以下是您的代码片段:

"PLEASE TELL ME A QUOTE",
"PLEASE TELL ME QUOTE"          # Missing Quote
"PLEASE TELL QUOTE",

但是第二个之后没有逗号,所以python通过将两个字符串连接在一起来解释这个,并在函数上读取它:

"PLEASE TELL ME A QUOTE",
"PLEASE TELL ME QUOTEPLEASE TELL QUOTE",