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())
2 ответов
+ 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"))
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())