Little sister's vocaulary

Is there sth wrong with replace method doesn’t let me to get the expected result?

def remove_suffix_ness(word):
    consonants = "bcdfghjklmnpqrstvwxyz"
    actual_word = word[:-4]
    if actual_word[-2] in consonants:
        return actual_word
    elif actual_word[-2] not in consonants:
        actual_word = actual_word.replace(actual_word[-1], 'y')
        return actual_word
    else:
        return word

Is there a test that fails when you do that? Do you see a “TEST FAILURE” variant you can expand to see details? That should show you any inputs that cause the test to fail along with the expected and actual result. Sharing those details (as text in a codeblock and not as a screenshot) should help!

TEST FAILURE

One or more variations of this test failed. Details can be found under each [variant#].

TEST FAILURE

AssertionError: 'heavi' != 'heavy'
- heavi
?     ^
+ heavy
?     ^
 : Called remove_suffix_ness("heaviness"). The function returned "heavi", but the tests expected "heavy" after the suffix was removed.

TSET FAILURE

AssertionError: 'say' != 'sad'
- say
?   ^
+ sad
?   ^
 : Called remove_suffix_ness("sadness"). The function returned "say", but the tests expected "sad" after the suffix was removed.
  1. Looking at that test output, do you understand what the test is doing?
  2. Looking at that test output, do you understand what the test is expecting and why?
  3. Looking at that test output, do you understand what your code returns and how it differs?
  4. Do you understand why your code is returning what it returns?

The answers to the first two questions is clear to me, but the other two is a bit vague.

That tells you what your code actually returned vs what was expected. Do you understand what your code is returning and why it is wrong?

No, I don’t understand why.

Can you explain what your code does, line by line, and what value is set with each assignment when the function is called with input “sadness”?

    # word is sadness
consonants = "bcdfghjklmnpqrstvwxyz" # sets the consonants variable to bcdfghjklmnpqrstvwxyz"
    actual_word = word[:-4] # sets actual word to ???
    if actual_word[-2] in consonants: # true or false?
        return actual_word # runs? or is skipped?
    elif actual_word[-2] not in consonants:
        actual_word = actual_word.replace(actual_word[-1], 'y') # runs? Skipped? Sets actual word to ???
        return actual_word
    else:
        return word # runs or is skipped?

I have issue as well
My code

def remove_suffix_ness(word):

suffix = 'ness'
result = []
for a in word:
    if a.endswith(suffix):
        root = a[:-4]
        if len(root) >1 and root[-1] == 'i' and root[-2] not in 'aeiou':
            root = root[:-1] + 'y'
        result.append(root)
    else:
        result.append(a)
return result

It’s working locally. But it’s failing in the Editor. What is wrong? Help me please

input_data = ['heaviness', 'sadness', 'softness', 'crabbiness', 'lightness', 'artiness', 'edginess']
result_data = ['heavy', 'sad', 'soft', 'crabby', 'light', 'arty', 'edgy']

for variant, (word, expected) in enumerate(zip(input_data, result_data), start=1):
    with self.subTest(f'variation #{variant}', word=word, expected=expected):
        actual_result = remove_suffix_ness(word)
        error_message = (f'Called remove_suffix_ness("{word}"). '
                         f'The function returned "{actual_result}", '
                         f'but the tests expected "{expected}" after the '
                         'suffix was removed.')

        self.assertEqual(actual_result, expected, msg=error_message)

What does “working locally” mean? Are you running the unit tests locally?

If you look at the tests, there should be variants with more specific/helpful errors. You may need to scroll down. Click on “TEST FAILURE” to expand variants the read/share the CODE RUN and TEST OUTPUT details.

def remove_suffix_ness(word):
    consonants = "bcdfghjklmnpqrstvwxyz"  # assigning consonants as value to consonant variable
    actual_word = word[:-4]  # the suffix has 4 letters, so the actual word would be from index 0 to the begining of the suffix (suffix is not included.)
    if actual_word[-2] in consonants: #here as the instructions says if the actual word ends in a consonat followed by 'y', there is a change of 'y' to 'i'
        return actual_word
    elif actual_word[-2] not in consonants:
        actual_word = actual_word.replace(actual_word[-1], 'y')  # In this line I used replace method to replace the 'i' with 'y' when the actual word doesn't end in consonant
        return actual_word
    else:
        return word

Could you show exact string values that the variable would have at each line if the function was called with sadness? I’m not asking you to explain the code. I’m asking what the variable contains, eg if you were to print() the lefthand side of every var = val operation.

Imagine if var = val printed the variable name and value every time it was evaluated. What would your code print when called with "sadness"?

It printed out “say”.
I changed elif part and it printed out “sadness”:

elif actual_word[-2] not in consonants and actual_word[-1] == 'i':

What is the value on every line?

def remove_suffix_ness(word):    # word is "sadness"
consonants = "bcdfghjklmnpqrstvwxyz" # consonants is "bcdfghjklmnpqrstvwxyz"
    actual_word = word[:-4] # actual_word is ???
    if actual_word[-2] in consonants: # condition is True? False?
        return actual_word # actual_word is ???
    elif actual_word[-2] not in consonants:  # condition is True? False?
        actual_word = actual_word.replace(actual_word[-1], 'y') # actual_word is ???
        return actual_word
    else:  # condition is True? False?
        return word # runs or is skipped?
def remove_suffix_ness(word):
    consonants = "bcdfghjklmnpqrstvwxyz" 
    actual_word = word[:-4]  #sad
    if actual_word[-2] in consonants: # actual_word[-2] is 'a' so it is skipped => False
        return actual_word
    elif actual_word[-2] not in consonants:  # True
        actual_word = actual_word.replace(actual_word[-1], 'y')  # it would be 'say'
        return actual_word # returns 'say'
    else: # skipped
        return word # skipped

I think I should consider a condition for the length of the word too.

It looks like you know exactly why your function returns say. You explained it quite well :slight_smile:

When called with sadness your code is supposed to return sad. Which is what you set it to on the second line of the function. Why does your function not return sad? What can you change to fix that?

1 Like

I made a few changes in my code, I found it logically wrong.
I edited the code like this, it runs in my IDE but it doesn’t work here.

def remove_suffix_ness(word):
    consonants = "bcdfghjklmnpqrstvwxyz"
    actual_word = word[:-4]
    if actual_word[-2] in consonants and actual_word[-1] == 'i':
        actual_word = actual_word.replace(actual_word[-1], 'y')
        return actual_word
    elif actual_word[-2] not in consonants:
        actual_word = actual_word
        return actual_word
    else:
        return word

I mean it outputs the expected result but not here.

I think the elif part is unnecesry, isn’t it?

I deleted it and it worked. :laughing:
Thanks a million isaac.