Python Forum
Check if string is uppercase or lowercase and eliminate
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Check if string is uppercase or lowercase and eliminate
#1
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
Reply
#2
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Sad Check if a given string has its open brackets closed by the same type of brackets? noahverner1995 1 1,193 Apr-11-2022, 02:13 PM
Last Post: deanhystad
  Using re to find only uppercase letters ranbarr 6 3,214 May-31-2021, 03:19 PM
Last Post: ranbarr
  Uppercase problem MarcJuegos_YT 4 2,568 Aug-21-2020, 02:16 PM
Last Post: MarcJuegos_YT
  Finding Number of Lowercase letters in a Set Steven 6 5,041 May-26-2017, 03:11 PM
Last Post: Steven

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020