Reputation: 33
I working on a school project. I am confused on how to make the ( if ) statement detect a character with upper and lower cases. I have tried to use 'and' in an if statement but it would be too long. My current input is:
a=input('Line: ')
if 'robot' in a:
print('There is a small robot in the line.')
elif ('robot'.upper()) in a:
print('There is a big robot in the line.')
elif b in a:
print('There is a medium robot in the line.')
else:
print('No robots here.')
Don't mind the (b) I was just figuring out something I don't know how to explain. An Output example I am looking for goes like:
There is a "robot" in the line
then it would print
'There is a robot in the line'
The program will check for both upper and lowercase characters. If an input had all caps, it would print out There is a big robot in the line If an input had just lowercase letters then it would just print there is a small robot in the line. If an input with both lower and upper cases it would print: There is a medium robot in the line.
Upvotes: 2
Views: 447
Reputation: 1528
Try this:
words=["robot","ROBOT","ROBot","APPLE","Nuts"]
res=["UPPER" if w.isupper() else ("LOWER" if w.islower() else "MIXEDCASE") for w in words]
print(res)
Output:
['LOWER', 'UPPER', 'MIXEDCASE', 'UPPER', 'MIXEDCASE']
Upvotes: 0
Reputation: 196
You want to use not islower() and not isupper() to get the mixed string check. Here is the code which works!
a=input('Line: ')
if 'robot' in a:
print('There is a small robot in the line.')
elif ('robot'.upper()) in a:
print('There is a big robot in the line.')
elif not a.islower() and not a.isupper():
print('There is a medium robot in the line.')
else:
print('No robots here.')
Upvotes: 0
Reputation: 749
How about using re
with IGNORECASE
to check the (b) situation. It means match the words like 'robot','Robot','rObot'...
import re
def check_robot(s):
if re.search(r"robot", s):
print("There is a small robot in the line.")
elif re.search(r"ROBOT", s):
print("There is a big robot in the line.")
elif re.search(r"robot",s,re.IGNORECASE):
print("There is a medium robot in the line.")
else:
print("'No robots here.'")
Upvotes: 0
Reputation: 106425
You can lowercase the input first before checking if the lowercase robot
is in the input string:
Change:
elif b in a:
to:
elif 'robot' in a.lower():
Upvotes: 2