简体   繁体   English

Python 3 AI:检查时间

[英]Python 3 AI : Check time

I'm creating an AI/Personal assistant/chatbot combination. 我正在创建AI /个人助手/聊天机器人组合。 It's working perfectly for chatting and answering question which I've inputted the answers for, and the code is rather simple. 它非常适合聊天和回答我输入了答案的问题,并且代码非常简单。

But I need to add a way so I can say "What is the time?" 但是我需要添加一种方法,以便我可以说“几点了?” (Or similar) and get a response telling me the accurate current time. (或类似结果),并得到一个响应,告诉我当前的准确时间。 That's most important but if possible, I'd like to have it able to check weather and perform as a calculator. 这是最重要的,但如果可能的话,我希望它能够检查天气并充当计算器。 I'll attach what I have so far. 我会附上我到目前为止所拥有的。 (Not nearly done, only about 15%). (几乎没有完成,只有大约15%)。

I'm using Python 3. I'm pretty new to Python and making this for a school project, so please don't be too complex with a solution, or explain it simply. 我正在使用Python3。对于Python来说,我是一个新手,它是为学校项目而设计的,所以请不要对解决方案太复杂,也不要简单地解释它。 Also, important: Where in the program will I put it? 同样重要的是:我将其放在程序中的什么位置?

And the code begins here: 代码从这里开始:

    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()

You've made it quite far, so I am just going to give you some guidance on printing the current time using Python and you should be able to fit this into your program wherever you decide it belongs. 您已经走了很远,所以我将为您提供一些有关使用Python打印当前时间的指导,并且无论您决定它属于哪个位置,都应该能够将其适合您的程序。

To return the current time for your timezone (you didn't specify the timezone you'd like to have), you can import the datetime library and ask it for the time, like so: 要返回您所在时区的当前时间(您未指定您想要的时区),您可以导入datetime库并询问其时间,如下所示:

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

This will probably give you the time in your timezone, a value something like 2016-02-01 16:37:30.591102 . 这可能会给您您所在时区的时间,类似于2016-02-01 16:37:30.591102

Optional Harder Next Step 可选更难的下一步

To be a bit more universal, you could prompt the user for a timezone offset, such as -8 (for PST here in California). 为了更通用一点,您可以提示用户输入时区偏移量,例如-8(适用于加利福尼亚的PST)。 One of the most straightforward ways to do that would be to take the time in UTC, using the following function: 最简单的方法之一是使用以下功能在UTC中花费时间:

now = datetime.datetime.utcnow()

And then you have to add or subtract the user's timezone offset, using a timedelta : 然后,您必须使用timedelta增加或减去用户的时区偏移量:

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

When you prompt the user for a timezone offset, the value you get back will be a str , but you will need it to be some kind of number in order to pass it to the timedelta function. 当提示用户输入时区偏移量时,返回的值将是str ,但是您需要将其设为某种数字才能将其传递给timedelta函数。 You'll need some way of turning a string into a number there if you want to go this route. 如果要走这条路,您将需要一些将字符串转换成数字的方法。

Another problem is that you would need to know the user asked your program the time question because you would have to immediately follow that question with the timezone offset question. 另一个问题是,您将需要知道用户向您的程序询问了时间问题,因为您必须立即将这个问题与时区偏移量问题联系在一起。

How to Print a Datetime object in interesting ways 如何以有趣的方式打印Datetime对象

Lastly, there are many different ways to format the value that now gives you in order to print it out in interesting ways. 最后,有许多不同的方法可以格式化now提供给您的值,以便以有趣的方式将其打印出来。 We often use the strftime function, which you can read as "string of time", and we pass it a formatting string so the function knows the format we want. 我们经常使用strftime函数,您可以将其读为“时间字符串”,并且将其传递给格式字符串,以便该函数知道所需的格式。 The formatting string uses specific identifiers, which you can see in the documentation : 格式字符串使用特定的标识符, 您可以在文档中看到这些标识符:

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

This will print the hour followed by a space followed by the minute followed by a space and the month as a 3-character abbreviation followed by a space and the day: 这将打印小时,后跟一个空格,然后是分钟,然后是一个空格和一个月,以3个字符的缩写形式,然后是一个空格和日期:

16 44 Feb 01

You can experiment with different ways of formatting the time using the examples in the documentation. 您可以使用文档中的示例尝试使用不同的格式化时间的方式。


The other things you asked about 您询问的其他事项

The weather and the calculator example are a bit more complex: the weather because you'll need to request it from a website and parse the results and the calculator because you'll need to write something that parses input and figures out what to do with it. 天气和计算器示例稍微复杂一点:天气,因为您需要从网站上请求并解析结果,而计算器,因为您需要编写一些东西来分析输入并弄清楚如何处理它。

I recommend playing with the time using these examples, and then, if you really want to tackle the calculator, then perhaps look at something on " reverse polish notation ", which make it so at least you won't have to match parentheses. 我建议使用这些示例,并花一些时间,然后,如果您真的想解决计算器问题,那么也许可以考虑一下“ 反向抛光符号 ”,这使得您至少不必匹配括号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM