0

reverse a word

I thought I had the coding right, but when I run, nothing appears. Please help. I need to reverse the lettering of a word that has more than 7 letters: def rev_words(sentence): sentence = ["Gravity is ridiculous"] for word in sentence.split(' '): if len(word) >= 7: sentence.append(word[::-1]) else: sentence.append(word) return ' '.join(sentence) print(rev_words())

5th Dec 2019, 11:25 PM
rae
2 Answers
+ 1
def rev_words(sentence): rev_word = [] for word in sentence.split(): if len(word) > 7: rev_word.append(word[::-1]) else: rev_word.append(word) return " ".join(rev_word) print(rev_words("Gravity is ridiculous"))
5th Dec 2019, 11:44 PM
rodwynnejones
rodwynnejones - avatar
0
split() works on strings not lists. so you did not have your coding right. you could have done this, with the same code: def rev_words(): sentence = ["Gravity is ridiculous"] for word in sentence[0].split(' '): if len(word) >= 7: sentence.append(word[::-1]) else: sentence.append(word) return ' '.join(sentence) print(rev_words())
6th Dec 2019, 12:01 AM
Bahha┣
Bahha┣ - avatar