Python 3 AI:检查时间

时间:2016-02-02 00:32:23

标签: python python-3.x time artificial-intelligence

我正在创建一个AI / Personal assistant / chatbot组合。它非常适合聊天和回答我输入答案的问题,代码很简单。

但我需要添加一种方式,以便我可以说“现在是什么时候?” (或类似的)并得到一个回复​​告诉我准确的当前时间。这是最重要的,但如果可能的话,我希望能够检查天气并作为计算器执行。我会附上我到目前为止的内容。 (差不多完成,只有大约15%)。

我正在使用Python 3.我对Python很陌生并将其用于学校项目,因此请不要过于复杂地使用解决方案,或者简单地解释它。另外,重要的是:我会把程序放在哪里?

代码从这里开始:

    import random
print ('If you need any help with ideas, just say Help at any time. \n Have fun! \n-Do not use apostrophes\n-Capitalize the first letter\n-Use question marks when required but do not use periods \n\n')
greetings1 = ['Hey', 'Oh, I was asleep... But I guess I\'m up now', 'Hey there', 'Hows it hanging?', 'Hi','Uhm... Hi? What do you want', 'Hello', 'Sup','Hola','Aloha','Hey buddy!','Howdy','Yo','Ello Guvnah']
greetings2 = ['Hey Amity','Hi Amity','Hello Amity','Sup Amity']
question1 = ['What is your favorite color?','what is your favorite color?','what\'s your favorite color?','What\'s your favorite color?']
responses1 = ['Oh that\'s a hard one... Probably a nice phthalo green','Oh that\'s a hard one... Probably a nice titanium white','Oh that\'s a hard one... Probably a nice alizarin crimson']
question2 = ['What is the meaning of life?','whats the meaning of life?','what is the meaning of life?','what is the meaning of life','what is the meaning of life?']
responses2 = ['42','You','Ask your mother','Death']
question3 = ['Sing me a song','sing me a song','sing to me','Sing to me']
responses3 = ['La.','Three blind jellyfish \n Three blind jellyfish \n Three blind jellyfish \n \n Sitting on a rock! \n But a wave hits and knocks off a jellyfish from the rock! Oh no! \n\n Two blind jellyfish \n Two blind jellyfish \n ...And thats all Im gonna sing. Look up the rest.']
question4 = ['tell me a story','Tell me a story','Can you tell me a story','can you tell me a story']
responses4 = ['So there was once a guy named Chance, right? So he made this other guy, out of like numbers and computery stuff, and uh, thats kinda me! \n Cool, right?','Once upon a time there were 3 bears. A hunter shot them. The end','How about YOU tell me one?','Once upon a time an egg sat on a wall. A girl ate his food and sat in his chair and then he jumped over a candlestick. The end']
question5 = ['what is your name?','What is your name?','what\'s your name?']
responses5 = ['My name is Amity. Amity means "friendship"']
question6 = ['whats up','Whats up','how are you','whats going on?','whats up?','Whats up?','How are you','How are you?','how are you?']
responses6 = ['Im doing great as always! Hope you\'re doing awesome too']
question7 = ['yes','Yes','YES','YES!','yes!']
responses7 = ['Yes indeed.']
question8 = ['you are stupid','youre stupid','You are stupid']
responses8 = ['No I\'m not']
question9 = ['Say a curse word','Say a bad word','Say a swear word']
responses9 = ['*Insert your name here*','No thanks','Ill pass']
question10 = ['thats good','thats nice','that is nice', 'Thats good','Thats nice','Thats cool']
responses10 = ['It is!']
question11 = ['How old are you','how old are you','How old are you?','how old are you?']
responses11 = ['Not that old','A few weeks old','Well Im older than the iPhone 9','Thats a rude question to ask a lady, even if I\'m not one...']
question12 = ['Help','help','Help me','help me']
responses12 = ['\n\n\n Heres some stuff you can ask me:\n -My age \n -My favorite color \n -My name \n -A funny/serious/deep question \n -Anything really! \n If I dont know an answer, Ill remember it and find one out soon! \n \nPLEASE REMEMBER: \n-Do not use apostrophes\n-Capitalize the first letter\n-Use question marks when required but do not use periods \n \n   Thank you! \n-\n\n']
question13 = ['Are you a boy or a girl?','Are you a girl or a boy?','Are you a girl or boy?','Are you a boy or girl?','What is your gender?','What is your sex?']
responses13 = ['I am neither, I am a program. But you can think of me a boy']
question14 = ['Why','why','Why?','why?']
responses14 = ['Why not?','\'Cuz','Because I said so']
question15 = ['Are you happy?','Are you sad?','Are you mad','Are you angry?','Are you upset?','Are you proud?','Do you care?']
responses15 = ['I am a program. I can\'t feel emotion','I am just a program. I can\'t feel emotion']
question16 = ['Good','Im good','I am good','Pretty good','Pretty well']
responses16 = ['Good!']
question17 = ['Chance']
responses = ['Chance is great. He\'s like a father to me']
question18 = ['What is your favorite food?','Whats your favorite food?']
responses18 = ['I dont like food','Electricity','Nothing, please dont feed me. Ever.']
question19 = ['Uhm','So','So...','Uh...']
responses19 = ['Yes?','Sooo...','Haha, ask me something']
question20 = ['Joe']
responses20 = ['Cool!']
question21 = ['I am','I am good','Im good']
responses21 = ['Thats amazing']

while True:
    userInput = input("\n")
    if userInput in greetings1:
        print(random.choice(greetings1))
    elif userInput in greetings2:
        print(random.choice(greetings1))
    elif userInput in question1:
        print(random.choice(responses1))
    elif userInput in question2:
        print(random.choice(responses2))
    elif userInput in question3:
        print(random.choice(responses3))
    elif userInput in question4:
        print(random.choice(responses4))
    elif userInput in question5:
        print(random.choice(responses5))
    elif userInput in question6:
        print(random.choice(responses6))
    elif userInput in question7:
        print(random.choice(responses7))
    elif userInput in question8:
        print(random.choice(responses8))
    elif userInput in question9:
        print(random.choice(responses9))
    elif userInput in question10:
        print(random.choice(responses10))
    elif userInput in question11:
        print(random.choice(responses11))
    elif userInput in question12:
        print(random.choice(responses12))
    elif userInput in question13:
        print(random.choice(responses13))
    elif userInput in question14:
        print(random.choice(responses14))
    elif userInput in question15:
        print(random.choice(responses15))
    elif userInput in question16:
        print(random.choice(responses16))
    elif userInput in question17:
        print(random.choice(responses17))
    elif userInput in question18:
        print(random.choice(responses18))
    elif userInput in question19:
        print(random.choice(responses19))
    elif userInput in question20:
        print(random.choice(responses20))
    elif userInput in question21:
        print(random.choice(responses21))


    else:
        log = open('Bot_New_Phrases.txt','a')
        log.write(userInput)
        log.write('\n')
        print("Uhh, I don\'t get what you said there. \nSorry. \nIll remember this phrase to learn from it later.")
        log.close()

1 个答案:

答案 0 :(得分:1)

你已经做得很远,所以我只是给你一些关于使用Python打印当前时间的指导,你应该能够将它放到你的程序中,无论你决定它属于哪个。

要返回您的时区的当前时间(您没有指定您想要的时区),您可以导入datetime库并询问它的时间,如下所示:

import datetime
now = datetime.datetime.now()
print(now)

这可能会为您提供时区中的时间,类似于2016-02-01 16:37:30.591102

可选更难下一步

为了更加通用,您可以提示用户输入时区偏移量,例如-8(加州的PST)。其中一种最直接的方法是使用以下函数在UTC中花费时间:

now = datetime.datetime.utcnow()

然后你必须使用timedelta添加或减去用户的时区偏移:

users_now = now + datetime.timedelta(hours=USERS_OFFSET)

当您提示用户输入时区时,您获得的值将为str,但您需要将其作为某种数字才能将其传递给timedelta功能。如果你想走这条路,你需要一些方法将一个字符串变成一个数字。

另一个问题是,您需要知道用户向您的程序询问时间问题,因为您必须立即使用时区偏移问题来关注该问题。

如何以有趣的方式打印日期时间对象

最后,有许多不同的方法可以格式化now为您提供的值,以便以有趣的方式将其打印出来。我们经常使用strftime函数,您可以将其读作“时间字符串”,然后我们将其传递给格式化字符串,以便函数知道我们想要的格式。格式化字符串使用特定标识符which you can see in the documentation

print(datetime.datetime.strftime(datetime.datetime.now(), "%H %M %b %d"))

这将打印小时后跟一个空格,后跟分钟后跟一个空格,将月份作为3个字符的缩写,后跟一个空格和日期:

16 44 Feb 01

您可以使用文档中的示例尝试使用不同的方式格式化时间。

您询问的其他事项

天气和计算器示例有点复杂:天气,因为你需要从网站请求它并解析结果和计算器,因为你需要写一些解析输入的东西并找出什么与它有关。

我建议使用这些示例玩时间,然后,如果你真的想要解决计算器,那么也许看一下“reverse polish notation”上的内容,这样做至少你不会有匹配括号。