So this is the brief: i need to input something like:
BaSe fOO ThE AttAcK
and return:
attack the base.
As you can see to decode this i need to start by reading the words in reverse order and if the first letter of the word isupper() then make it lowercase and append it to a list which i will later print. This is what i have so far:
# Enter your code for "BaSe fOO ThE AttAcK" here. default = input('code: ') listdefault = default.split() uncrypted = [] for i in range(len(listdefault)): if listdefault[:-i].istitle(): # doesn't work i = i.lower() # dont know if this works, Should convert word to lower case. uncrypted.append(i) solution = ' '.join(uncrypted) print(solution)
Can someone show me how to get this program to work? Says that i can’t use the istitle() method on a list type..
Advertisement
Answer
You’re close, but you’re switching between treating i
as an index (for i in range...
) and as the word itself (uncrypted.append(i)
). And you probably meant listdefault[i].istitle()
rather than listdefault[:-i].istitle()
.
The best solution is to change it to:
for w in listdefault[::-1]: if w.istitle(): w = w.lower() uncrypted.append(w)
Since listdefault[::-1]
is a way to reverse the list (reversed(listdefault)
also works). If you know how to use list comprehensions, you can do it in one line:
solution = ' '.join([w.lower() for w in listdefault[::-1] if w.istitle()])