Python Forum

Full Version: Check if string is uppercase or lowercase and eliminate
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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)
Output:
['BaSe', 'fOO', 'ThE', 'AttAcK'] 4 ['BaSe', 'fOO', 'ThE', 'AttAcK']
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
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()
True
Also 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)