【问题标题】:Python 3 AI : Check timePython 3 AI:检查时间
【发布时间】:2016-02-02 00:32:22
【问题描述】:

我正在创建一个人工智能/个人助理/聊天机器人组合。它非常适合聊天和回答我已经输入答案的问题,并且代码相当简单。

但我需要添加一种方式,以便我可以说“现在几点?” (或类似的)并得到回复,告诉我准确的当前时间。这是最重要的,但如果可能的话,我希望它能够检查天气并用作计算器。我会附上我到目前为止所拥有的。 (几乎没有完成,只有大约 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()

【问题讨论】:

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


    【解决方案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”上的内容,这样至少您不必匹配括弧。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-18
      • 1970-01-01
      相关资源
      最近更新 更多