Python 实现简单的对话机器人 预报天气

import time
import random
import requests
from termcolor import colored
from simpleeval import simple_eval


class Bot:
    wait = 1

    def __init__(self):
        self.q = ''
        self.a = ''

    def _think(self, s):
        return s

    def _format(self, s):
        return colored(s, 'blue')

    def run(self):
        time.sleep(Bot.wait)
        print(self._format(self.q))
        while True:
            self.a = input()
            if self.a != '':
                break
            print(self._format('Input valid, Please enter again!'))
        time.sleep(Bot.wait)
        print(self._format(self._think(self.a)))


class HelloBot(Bot):
    def __init__(self):
        self.q = 'Hi, What\'s your name?'

    def _think(self, s):
        return f'Hello, {s}!'


class GreetingBot(Bot):
    def __init__(self):
        self.q = 'Hi, what\'s your feeling now?'

    def _isgood(self, s):
        Pos = ['good', 'fine', 'ok', 'nice']
        tag = False
        for pp in Pos:
            if pp in s.lower():
                tag = True
        if 'not' in s.lower():
            tag = bool(1 - tag)
        return tag

    def _think(self, s):
        if self._isgood(s):
            return f'I feel fine, too!'
        else:
            return f'Sorry to hear that, Be cheerful! my friend.'


class FavColor(Bot):
    def __init__(self):
        self.q = 'Hi, what\'s your favorite color?'

    def _think(self, s):
        colors = ['white', 'purple', 'gray', 'pink']
        return f'Your favorite color is {s.lower()}, While my favorite is {random.choice(colors)}'


class WeatherBot(Bot):
    def __init__(self):
        self.q = 'Hello, I can do weather report for you! Please input city\'s name'

    def _think(self, s):
        city = self.a
        rep = requests.get(f'http://www.tianqiapi.com/api?version=v6&appid=23035354&appsecret=8YvlPNrz&city={city}')
        rep.encoding = 'utf-8'
        print('城市:%s' % rep.json()['city'])
        print('天气:%s' % rep.json()['wea'])
        print('风向:%s' % rep.json()['win'])
        print('温度:%s' % rep.json()['tem'] + '°C')
        print('风力:%s' % rep.json()['win_speed'])
        print('湿度:%s' % rep.json()['humidity'])
        print('空气质量:%s' % rep.json()['air_level'])
        return f'{city} forecast over'


class CalcBot(Bot):
    def __init__(self):
        self.q = 'Hello, my friend, I can do some scientific calculation for you'

    def calctime(self, num):
        # 假设此人每秒进行1e20次运算
        times = 1e20
        return num / times

    def run(self):
        Quit = ['x', 'q', 'exit', 'quit']
        while True:
            time.sleep(Bot.wait)
            print(self._format(self.q))
            self.a = input()
            if self.a.lower() in Quit:
                break
            try:
                result = simple_eval(self.a)
            except ZeroDivisionError as err:  # useful
                print(f'Oops!There are something unexpected happened:{err}, please enter again')
            else:
                self.wait = self.calctime(result)
                time.sleep(self.wait)
                print(self._format(f'Done! Result is {result}'))
        print(self._format('Calculation Over, Thanks for using'))


class AmazingPy(Bot):
    # 为了继承Bot中的_format方法不用重写
    def __init__(self, wait):
        Bot.wait = wait
        self.bots = []

    def add(self, bot):
        self.bots.append(bot)

    def _myprint(self, s):
        print(s)
        print()

    def run(self):
        self._myprint(self._format('Hi, I\'m AI assistant made by Hz. Let\'s talk!'))
        for bot in self.bots:
            bot.run()


HZ = AmazingPy(1)
HZ.add(HelloBot())
HZ.add(GreetingBot())
HZ.add(FavColor())
HZ.add(CalcBot())
HZ.add(WeatherBot())
HZ.run()
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值