私は単語 "banana"に "ana"のようなフレーズを入れることができ、フレーズが何回見つかったかを数えます言葉で。私のテストユニットの一部が動作しないようにするエラーを見つけることができません。私のカウント機能をPythonで動かすことができません
def test(actual, expected):
""" Compare the actual to the expected value,
and print a suitable message.
"""
import sys
linenum = sys._getframe(1).f_lineno # get the caller's line number.
if (expected == actual):
msg = "Test on line {0} passed.".format(linenum)
else:
msg = ("Test on line {0} failed. Expected '{1}', but got '{2}'.".format(linenum, expected, actual))
print(msg)
def count(phrase, word):
count1 = 0
num_phrase = len(phrase)
num_letters = len(word)
for i in range(num_letters):
for x in word[i:i+num_phrase]:
if phrase in word:
count1 += 1
else:
continue
return count1
def test_suite():
test(count('is', 'Mississippi'), 2)
test(count('an', 'banana'), 2)
test(count('ana', 'banana'), 2)
test(count('nana', 'banana'), 1)
test(count('nanan', 'banana'), 0)
test(count('aaa', 'aaaaaa'), 4)
test_suite()
:?
デフ(シーケンス、アイテム)を数えますかP.S.あなたの質問をより読みやすくするために余分な空白行を減らしてください。ありがとうございました。 –
word []のxの繰り返しは意味をなさない。 –
あなたの変数名は非常に混乱しています。例えば、 'num_phrase'はフレーズの数ではなく、その長さです。*' x'は*完全に*記述的ではありません。私の経験では、用語を整理することは、短期間で問題を明らかにする傾向があります。 – kindall