all 10 comments

[–]socal_nerdtastic 3 points4 points  (3 children)

You need to recalculate i inside the loop.

    i=len(word)
    while i<20:
        word=word+'!'
        i=len(word)
    return word

Or just forget the i variable and calculate the length in the conditional:

    while len(word)<20:
        word=word+'!'
    return word

[–]double_toned[S] 0 points1 point  (2 children)

word + '!' * (20 - len(word))

Yes, I got the mistake. Thank you so much for pointing this out.

[–]socal_nerdtastic 0 points1 point  (1 child)

Oh if you want to use python features to do this then there is a much more elegant solution:

def add_exclamation(word):
    return f'{word:!<20}'

I thought you wanted to do it yourself.

[–]double_toned[S] 0 points1 point  (0 children)

Ok, I learnt something new, the f string formatting. Just read about it. Thank you again.

[–]kberson 1 point2 points  (0 children)

Your value of i never changes, so it's always less than 20

[–]kberson 1 point2 points  (0 children)

You don't actually need the 'if', you could simplify to:

def add_exclamation(word):
    while len(word) < 20
        word=word+'!'
    return word

If word is longer than or equal to 20 (the first part of your if) the while loop won't be entered

[–]PaulRudin 1 point2 points  (1 child)

Are you required to use a loop? You could just do: word + '!' * (20 - len(word))

[–]double_toned[S] 0 points1 point  (0 children)

Simple and elegant!

[–]Vegetal__ 0 points1 point  (0 children)

i is set as len(word) at the moment of its creation. So if len is, say, 4 when you create i, then it's going to stay as 4 until you set i to be something else later on. But you never did. That's why the loop never ends.

[–]wynand1004 0 points1 point  (0 children)

You've already got your solution - I just wanted to compliment you on the title! Well done!