Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于出现 None 的问题 #99

Closed
Osacr58 opened this issue Oct 27, 2020 · 1 comment
Closed

关于出现 None 的问题 #99

Osacr58 opened this issue Oct 27, 2020 · 1 comment

Comments

@Osacr58
Copy link

Osacr58 commented Oct 27, 2020

写了一段代码,在运行的时候总会先给出None,再给出计算机的答案,向老师求教

from time import sleep
from termcolor import colored
import random

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):
    sleep(Bot.wait)
    print(self._format(self.q))
    self.a = input()
    sleep(Bot.wait)
    print(self._format(self._think(self.a)))

class ConfirmBot(Bot):
def init(self):
self.q = "Do you like soccer?"

def _think(self, s):
        print("Whether you like it nor not, let's talk something about soccer! I'm really interested in it!")

class TeamBot(Bot):

def __init__(self):
    self.q = "Buddy, what's your favorite team in the Chinese Super League?"

def _think(self, s):
    if 'sipg' in s.lower():
        print("Wow! We are both fans of Shanghai SIPG! Nice to talk with you!")
    else:
        print("Pity, as you can see, we're opponents...")

class CompareBot(Bot):

def __init__(self):
    self.q = "Which one of the team in the Chinese Super League do you think will win the championship?"

def _think(self, s):
    teams = ['Shenzhen Jiazhaoye', 'Shandong Luneng', 'Beijing Guoan', 'Shanghai Greenland Shenhua', 
             'Guangzhou Evergrand', 'Chongqing SWM Motors', 'Tianjin Teda', 'Henan Jianye', 'Jiangsu Suning',
             'Dalian Pro', 'Wuhan Zall', 'Guangzhou Fuli', 'Hebei HX Xingfu', 'Shijiazhuang yongchang', 
             'Qingdao Huanghai', 'Shanghai SIPG']
    
    return f"I hope that {random.choice(teams)} will win."

class Oscar:

def __init__(self, wait=1):
    Bot.wait = wait
    self.bots = []

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

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

def run(self):
    self._prompt("This is Oscar dialog system. Let's talk.")
    for bot in self.bots:
        bot.run()

oscar = Oscar(1)
oscar.add(ConfirmBot())
oscar.add(TeamBot())
oscar.add(CompareBot())
oscar.run()

@neolee neolee changed the title 关于出现None的问题 关于出现 None 的问题 Oct 27, 2020
@neolee
Copy link
Owner

neolee commented Oct 27, 2020

首先,仔细学习 https://github.com/neolee/wop-ecnu-pub/wiki , 学会如何在这里贴代码。

你遇到的问题的原因在于:你的有几个 bot 的 _think 方法没写对。这个方法应该返回一个字符串,但你有几个写的没有返回字符串,而是 print() 了一句话出来,这样一来,run() 方法里调用 print(self._format(self._think(self.a))) 这一句就会打印出一个 None(想想为什么)。

@neolee neolee closed this as completed Nov 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants