Check if string is uppercase or lowercase and eliminate - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Check if string is uppercase or lowercase and eliminate (/thread-7261.html) |
Check if string is uppercase or lowercase and eliminate - Wolfpack2605 - Jan-01-2018 In the war against Skynet, humans are trying to pass messages to each other without the computers realising what's happening. To do this, they are using a simple code: They read the words in reverse order They only pay attention to the words in the message that start with an uppercase letter So, something like: BaSe fOO ThE AttAcK contains the message: attack the base However, the computers have captured you and forced you to write a program so they can understand all the human messages (we won't go into what terrible tortures you've undergone). Your program must work as follows: code: soMe SuPPLies liKE Ice-cREAm aRe iMPORtant oNly tO THeir cReaTORS. tO DestroY thEm iS pOInTLess. says: destroy their ice-cream supplies Notice that, as well as extracting the message, we make every word lowercase so it's easier to read. This is what I have written so far... output=[] b=0 d=0 code=input("code: ") code=code.split() print(code) a=len(code) print(a) while b<a: c=code[b] if c.isupper: output.append(c) b=b+1 elif c.islower: b=b+1 else: b=b+1 print(output) I need the last line to say "BaSe ThE AttAck" eliminating "fOO" and I will be reversing the string in the last step to make sense, but it is not differentiating between a lowercase word and an uppercase word.Please help. Thanks RE: Check if string is uppercase or lowercase and eliminate - Mekire - Jan-01-2018 isupper is a function not an attribute so it needs parenthesis>>> "Fred".isupper <built-in method isupper of str object at 0x0348DEC0> >>> "Fred".isupper() False >>> "Fred"[0].isupper() TrueAlso you should be using for loops for this, not while loops:code = "soMe SuPPLies liKE Ice-cREAm aRe iMPORtant oNly tO THeir cReaTORS. tO DestroY thEm iS pOInTLess" for word in code.split(): if word[0].isupper(): print(word) |