猫史档案馆


【Python作品分享】学习AI(学习功能研发中)【作品秀】

用户:Air_Air_查看:0 回复:0 评论:0 创建时间:2022-05-03T11:50:49


【作品展示】

center_image

 

【作品介绍】

目前做了以下几项功能:

1.时间与日期

2.笑话

3.天气

欢迎完善!

 

【作品源代码】

'''
{
    'data':{
        'yesterday':{
            'date': '2日星期一',
            'high': '高温 21℃',
            'fx': '东北风',
            'low': '低温 15℃',
            'fl': '2级',
            'type': '阴'
        },
        'city':'东莞',
        'forecast':[
            {
                'date':'3日星期二',
                'high': '高温 27℃',
                'fengli': '2级',
                'low': '低温 18℃',
                'fengxiang': '东北风',
                'type': '晴'
            },
            {
                'date': '4日星期三',
                'high': '高温 29℃',
                'fengli': '2级',
                'low': '低温 20℃',
                'fengxiang': '东北风',
                'type': '晴'
            },
            {
                'date': '5日星期四',
                'high': '高温 30℃',
                'fengli': '2级',
                'low': '低温 20℃',
                'fengxiang': '东南风',
                'type': '晴'
            },
            {
                'date': '6日星期五',
                'high': '高温 30℃',
                'fengli': '2级',
                'low': '低温 21℃',
                'fengxiang': '东南风',
                'type': '阴'
            },
            {
                'date': '7日星期六',
                'high': '高温 30℃',
                'fengli': '2级',
                'low': '低温 23℃',
                'fengxiang': '东南风',
                'type': '中雨'
            }],
        'ganmao': '感冒低发期,天气舒适,请注意多吃蔬菜水果,多喝水哦。',
        'wendu': '20'
    },
    'status': 1000,
    'desc': 'OK'
}
'''
import tkinter as tk
from datetime import datetime
import random as rd
import requests
window = tk.Tk()
window.geometry('400x400')
window.title('学习AI')

t1Lbl = tk.Label(window,text='输入内容')
t1Lbl.place(x=0,y=10)

t2Lbl = tk.Label(window,text='回复内容')
t2Lbl.place(x=0,y=100)

inputEty = tk.Entry(window,width = 40)
inputEty.place(x=12,y=40)


def getToday(cityName):
	url = 'http://wthrcdn.etouch.cn/weather_mini?city=' + cityName
	response = requests.get(url)
	weatherDict = response.json()
	print(weatherDict)
	if weatherDict['desc'] == 'OK':
		return weatherDict
	else:
		return 'Error: %s city is not find.'

def AI():
    global AILbl, now, a
    cmd = inputEty.get()
    
    now = datetime.now()
    li = {
        '时间':now.strftime('现在是%Y年%m月%d日 %H:%M。'),
        '几点':now.strftime('现在%H:%M了!'),
        '日期':now.strftime('今天是%Y年%m月%d日,现在是%H:%M。'),
        '笑话':rd.choice(['一次,英语老师考完试问我:“考完英语最大的感受是什么?”\n “我的心从未如此清晰的知道自己是中国人”我回答。','大人们看见司马光砸缸,上前问道:“你怎么不砸点别的,司马?','我不会讲笑话,只会卖萌,呵呵!','一天,一位记者看到一位正在晨跑的大爷\n“大爷,我现场采访您一下,您这样晨跑锻炼坚持几年了?”\n谁知大爷不耐烦地答道:“姑娘别挡道!我尿急! ”',
            '''中午去买菜,感觉都不太新鲜了。
老板:“早上刚到的,都新鲜的。”
我:“这菜看着就蔫蔫的啊?!”
老板:“从早上到现在,它以为没人要自己了,这不垂头丧气么!”
我:。''']),
    }

    def weather():
        global a
        b = 0
        for d in cmd:
            if d == '的':
                c = cmd[:b]
                break
            b += 1
        td = getToday(c)
        try:
            a = '%s今日的天气%s,\n气温%s度,\n%s,\n%s,\n%s,\n%s' % (td['data']['city'],td['data']['forecast'][0]['type'],td['data']['wendu'],td['data']['forecast'][0]['high'],td['data']['forecast'][0]['low'],td['data']['forecast'][0]['fengxiang'],td['data']['ganmao'])
        except:
            a = td % c
    
    le = {
        '的天气':weather
    }

    a = None

    for k in li.keys():
        if k in cmd:
            a = li[k]
            break

    for k in le.keys():
        if k in cmd:
            le[k]()
            break

    if not a:
        er = ['对不起,我听不懂你在说什么。','人类的语言太深奥了!','哎,还是回去补习吧~','这明显超出了我的能力范围。']
        a = rd.choice(er)

    try:
        if AILbl:
            AILbl.destroy()
    except:
        print('')

    AILbl = tk.Label(window,text=a,fg = 'blue',anchor='nw')
    AILbl.place(x=13,y=130)

okBtn = tk.Button(window,text = '确定',width = 10,height = 1,command = AI)
okBtn.place(x=290,y = 75)

window.mainloop()

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页