2015-10-24 3 views
0

Я делал курс Python в Google: https://developers.google.com/edu/python/stringsPython - замена строки

Ссылка на упражнения: https://developers.google.com/edu/python/google-python-exercises.zip

Упражнение: ./google-python-exercises/basic/strings2.py

на следующий экзамен:

# E. not_bad 
# Given a string, find the first appearance of the 
# substring 'not' and 'bad'. If the 'bad' follows 
# the 'not', replace the whole 'not'...'bad' substring 
# with 'good'. 
# Return the resulting string. 
# So 'This dinner is not that bad!' yields: 
# This dinner is good! 
def not_bad(s): 
    +++your code here+++ 
    return 

Мой ANSW эр был:

def not_bad(s): 
    not_position = s.find('not') 
    bad_position = s.find('bad') 
    if bad_position > not_position: 
    s = s.replace(s[not_position:],'good') 
    return s 

Когда я побежал проверки я получил следующее:

not_bad 
OK got: 'This movie is good' expected: 'This movie is good' 
    X got: 'This dinner is good' expected: 'This dinner is good!' 
OK got: 'This tea is not hot' expected: 'This tea is not hot' 
OK got: "It's bad yet not" expected: "It's bad yet not" 

Я считаю, что «Этот обед хорошо» == «Этот обед хорошо», но я не уверен, почему я не получаю статус «ОК», но «Х». Я считаю, что я не сдавал экзамен правильно, но результат все еще правильный. Я новичок в Python, поэтому комментарии по этому поводу будут высоко оценены!

+0

'хороший = хороший'!. обратите внимание на '!' – sam

+0

Приятный момент, я попытаюсь исправить это –

ответ

3

Вы пропустили восклицательный знак ! в ожидаемом ответе. Одним из способов исправить ваше решение было бы указать и включить конечный индекс замененной подстроки, используя результат поиска bad.

1

мне удалось решить это:

def not_bad(s): 
    not_position = s.find('not') 
    bad_position = s.find('bad') 
    if bad_position > not_position: 
    s = s.replace(s[not_position:bad_position+3],'good') 
    return s 

not_bad 
OK got: 'This movie is good' expected: 'This movie is good' 
OK got: 'This dinner is good!' expected: 'This dinner is good!' 
OK got: 'This tea is not hot' expected: 'This tea is not hot' 
OK got: "It's bad yet not" expected: "It's bad yet not" 
Смежные вопросы