gpt4 book ai didi

python - 锻炼 Python 鲍勃

转载 作者:太空宇宙 更新时间:2023-11-04 00:46:47 24 4
gpt4 key购买 nike

我正在通过 Exercism.IO 学习 Python,我目前正在处理 Bob 问题,问题的对象如下:

Bob is a lackadaisical teenager. In conversation, his responses are very limited. Bob answers 'Sure.' if you ask him a question. He answers 'Whoa, chill out!' if you yell at him. He says 'Fine. Be that way!' if you address him without actually saying anything. He answers 'Whatever.' to anything else.

到目前为止,我已经通过了一些测试,但我被困在它应该返回 whatever 但所有字符都是整数的地方,所以它当然不起作用。

这是我失败的地方:

def test_only_numbers(self):
self.assertEqual(
'Whatever.', bob.hey('1, 2, 3')
)

所有字符都是整数,我测试它们是否在大喊大叫看起来像这样:

def is_yelling(self):
return self.sentence == self.sentence.upper()

很明显,字符在大写或小写时是相同的,因为它们是数字,所以程序认为它们在大喊大叫。我的问题是如何重构这个程序,以便当断言全是数字时,它不会将其视为大喊大叫?

def hey(what):
sentence = SentenceThinker(what)
if sentence.is_silence():
return "Fine. Be that way!"
elif sentence.is_yelling():
return "Whoa, chill out!"
elif sentence.is_question():
return "Sure."
else:
return "Whatever."


class SentenceThinker(object):

def __init__(self, sentence):
self.sentence = sentence

def is_yelling(self):
return self.sentence == self.sentence.upper()

def is_question(self):
return self.sentence.endswith("?")

def is_silence(self):
return not self.sentence

最佳答案

考虑使用内置的字符串方法 str.isupper()

关于python - 锻炼 Python 鲍勃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39216804/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com