Some context:
I have never tried to program anything before 3 days ago, so I am a complete beginner. I am trying to learn python, and I am learning through a free module on Grok learning There is an answer to a question that I have mostly figured out but can't finish. The module is for an introductory course so I am confident that there is a fairly simple solution, but everything ive seen for similar situations was over my head.
The problem is: "Robots in a line!
The robots are invading (your written work)! Robots are sneaking into your text files. Write a program that reads in a line of text from the user and prints out whether there is a robot in the line of text.
If the word robot occurs in all lowercase letters in the line, print out: There is a small robot in the line.
If the word ROBOT occurs in all uppercase letters in the line, print out: There is a big robot in the line.
If the word robot occurs in the line in any combination of upper and lowercase letters, print out: There is a medium sized robot in the line. Otherwise, if none of these conditions hold true, you should print out: No robots here. Your program should work like this:
Line: I'm baking chocolate robot brownies. There is a small robot in the line. Here is another example:
Line: Look at the rOBOt down the road. There is a medium sized robot in the line. If the letters robot occur in the line, but as part of a bigger word, you should not report any robots found."
Line: There's a strobotron at the concert. No robots here. My solution thus far:
line = input("Write something:") lr = ("There is a small robot in the line.") br = ("There is a big robot in the line.") mr = ("There is a medium sized robot in the line.") nr = ("No robots here.") check1 = ("robot" in line) check2 = ("ROBOT" in line) lowcase = check1 upcase = check2 if(lowcase == True): print(lr) elif(upcase == True): print(br) else: print(nr) Please remember I am a complete beginner so your solution may need a little bit of explaining, and feel free to critique the code I've already written, but so far it seems to work. Thank you for taking the time to read all this and help.
6 Answers
How about using str.lower or str.casefold:
>>> 'Look at the rOBOt down the road.'.lower() 'look at the robot down the road.' >>> 'robot' in 'Look at the rOBOt down the road.' # check1 False >>> 'ROBOT' in 'Look at the rOBOt down the road.' # check2 False >>> 'robot' in 'Look at the rOBOt down the road.'.lower() True if lowcase: print(lr) elif upcase: print(br) elif 'robot' in line.lower(): print(mr) else: print(nr) 0The hardest part is when robot appears in part of a bigger word, since it is easy to lookup for "robot" in the string but abit harder to check if there are boundaries at both side of it.
Using only builtins:
BOUNDARIES = " .,?!:;'\"" while True: string = input("Write something or hit Enter to quit: ") # This breaks the loop (quits the program) if the user hit # an Enter. if not string: break # First we look robot in the lowercased string. part = string.lower().partition("robot") # If a robot is in the string, the middle part of part # is going to be "robot". If not, it is an empty string. if not part[1]: print("No robots here.") # If the end of part[0] and the beginning of part[2] is # not in BOUNDARIES then we still have no robots there. elif ((not part[0] or not part[0][-1] in BOUNDARIES) and (not part[2] or not part[2][0] in BOUNDARIES)): print("No robots here.") # Now we look for small and big robots in the original # string. elif "robot" in string: print("There is a small robot in the line.") elif "ROBOT" in string: print("There is a big robot in the line.") # If we are here that is because of a medium robot. else: print("There is a medium sized robot in the line.") Using regular expressions make it abit shorter/cleaner. However your program will start a little slower because it needs to import the re module first:
import re PATTERN = r"\b[rR][oO][bB][oO][tT]\b" while True: string = input("Write something or hit Enter to quit: ") if not string: break search = re.search(PATTERN, string) if not search: print("No robots here.") elif search.group() == "robot": print("There is a small robot in the line.") elif search.group() == "ROBOT": print("There is a big robot in the line.") else: print("There is a medium sized robot in the line.") Test run:
Write something or hit Enter to quit: I'm baking chocolate robot brownies. There is a small robot in the line. Write something or hit Enter to quit: Look at the rOBOt down the road. There is a medium sized robot in the line. Write something or hit Enter to quit: There's a strobotron at the concert. No robots here. Write something or hit Enter to quit: I have a robot. There is a small robot in the line. Write something or hit Enter to quit: The code on it's plate was: "ROBOT 142/1". There is a big robot in the line. Write something or hit Enter to quit: Here is one way of doing it.. just using the things that they teach you in that course. i know its not the smartest way but its simple and it works! :)
# Enter your code for "Robots in a line!" here. str1 = input('Line: ') words = str1.split() if 'ROBOT' in words: print('There is a big robot in the line.') elif 'robot' in words: print('There is a small robot in the line.') elif 'robot' in str1.lower().split(): print('There is a medium sized robot in the line.') else: print('No robots here.') Just using the Grok lectures
line = input("Line: ") line1 = line.lower() #mixed case will be converted to lowercase line = line.split() line1 = line1.split() #split line1 to see if the word robot exists if "robot" in line: print("There is a small robot in the line.") elif "ROBOT" in line: print("There is a big robot in the line.") elif "robot" and "ROBOT" not in line and "robot" not in line1: #checking if lower case, upper case, and mixed case converted robot exist print("No robots here.") else: print("There is a medium sized robot in the line.") You may try as below, but this involves multiple checks.
s = "I'm baking chocolate roBOT brownies" lower_text = 'robot' normal_split = s.split() lower_split = s.lower().split() if lower_text in normal_split: # check if lower case robot is present in normal split print('There is a small robot in the line.') elif lower_text.upper() in normal_split: # check if upper case robot is present in normal split print('There is a big robot in the line.') elif lower_text not in lower_split: # check if atleast any case robot is present in the split print('No robots here.') else: # if none of the above matches then medium size robot print('There is a medium sized robot in the line.') Cheers!
You can use the following to find mixed case words in a Python string:
str1 = input("Line: ") words = str1.split() if "ROBOT" in words: print("There is a big robot in the line.") elif "robot" in words: print("There is a small robot in the line.") elif "robot" in str1.lower().split(): print("There is a medium sized robot in the line.") else: print("No robots here.")