-
-
Save mayankdawar/ffbe007ca4344cda8133e32069df42ba to your computer and use it in GitHub Desktop.
words = ["adopt", "bake", "beam", "confide", "grill", "plant", "time", "wave", "wish"] | |
past_tense = [] | |
for i in words: | |
if(i[len(i)-1] == 'e'): | |
i += 'd' | |
else: | |
i += 'ed' | |
past_tense.append(i) |
I just want to add my thoughts on the matter as a late arrival :)
words = ["adopt", "bake", "beam", "confide", "grill", "plant", "time", "wave", "wish"]
past_tense = []
for i in words:
if i[-1] == "e":
i += "d"
past_tense.append(i)
else:
i += "ed"
past_tense.append(i)
We can test/debug if it's working by adding a "print" statement after each conditional:
for i in words:
if i[-1] == "e":
i += "d"
past_tense.append(i)
print(i) >>>>>>>>>>>>>>> DEBUG
else:
i += "ed"
past_tense.append(i)
print(i) >>>>>>>>>>>>>>> DEBUG
Thanks and happy learning!
words = ["adopt", "bake", "beam", "confide", "grill", "plant", "time", "wave", "wish"]
past_tense = []
for word in words:
#print(word)
if word[-1] == 'e':
word += 'd'
past_tense += [word]
else:
word += 'ed'
past_tense += [word]
A step-by-step guide I made:
words = ["adopt", "bake", "beam", "confide", "grill", "plant", "time", "wave", "wish"]
past_tense = [] #create an empty list.
for word in words: #create an interative for every word in the list "words".
if word[-1] == "e": #conditional statement for last letter of word being "e".
word += "d" #add "d" if ends in "e" and update the value of "word".
past_tense.append(word) #paste the last record of "word" in the list "past_tense".
else: #otherwise, if the last letter of "word" is not "e".
word += "ed" #add "ed" to "word" and update its value.
past_tense.append(word) #add the last recorded value of "word" in the list.
print(past_tense) #print the "past_tense" list of new words.
Hi!. I realized adding little bit of explanation will help new coder's to understand the code better. :)
words = ["adopt", "bake", "beam", "confide", "grill", "plant", "time", "wave", "wish"]
//creating an empty list named "past_tense"
past_tense = []
for i in words:
if i[-1] == "e": // i[-1] indicates the last element of the i's
i = i+ "d"
past_tense.append(i) // The append() method adds an item to the end of the list.
else:
i = i + "ed"
past_tense.append(i)
print(past_tense)
words = ["adopt", "bake", "beam", "confide", "grill", "plant", "time", "wave", "wish"]
past_tense=[]
for i in words:
if i[-1]=="e":
past_tense.append(i+"d")
else:
past_tense.append(i+"ed")
> if word[len(word)-1] == 'e':
word[-1] would be enough