2017-01-25 61 views
-2

我已經開始在python中創建一個AI,並且使用os.system(我正在使用一個Mac!)的BASh命令。人工智能從類似命令的工作:Python - 數組的某些部分被忽略?爲什麼?固定?

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

選擇是importfrom 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 ^^^     # 
################################################### 
+2

您至少缺少兩個逗號,如果您使用一致的格式,您可能會注意到這兩個逗號。 –

+0

就像@JaredSmith說的,你在你的'uiQuoteAsk'列表中錯過了2個逗號。 –

回答

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", 
相關問題