1
r = input("Line: ")
r = r.split()
for rs in r:
  if rs == "robot":
    print("There is a small robot in the line.")
  elif rs == "ROBOT":
    print("there is a big robot in the line.")
  elif rs[0] == "r" or "R" and rs[1] == "o" or "O" and rs[2] == "b" or "B" and rs[3] == "O" or"o" and rs[-1] == "T" or "t":
    print("There is a medium sized robot in the line.")
  else:
    print("There are no robots in the line.")

This works but I get an error statement that says that the string index is out of range?? Any ideas?

Tom Zych
  • 13,329
  • 9
  • 36
  • 53
user3864194
  • 83
  • 2
  • 6

5 Answers5

1

checking rs[2] == 'b' demands that all words in your line are at least 3 chars long...

John Greenall
  • 1,670
  • 11
  • 17
0

Your error is occurring because your string may not be long enough. Most likely this is on your medium case check where you are using indexes.

One option you have is to use the istitle method to check your "medium case". This provides a way to accomplish your goal:

r = input("Line: ")
r = r.split()
for rs in r:
    if "robot" in rs.lower():
        if rs == "ROBOT":
            result = "BIG ROBOT"
        if rs == "robot":
            result = "Little Robot"
        if rs.istitle():
            result = "Medium Robot"
    else:
        result = "No robots"
print result

Outputs:

Line: a robot
Little Robot

Line: a Medium Robot
Medium Robot

Line: a large ROBOT
BIG ROBOT

Line: No automatic machines
No robots

One thing you'll notice about this code is that I'm saving the results until the end of the loop. Otherwise, you'll get multiple lines of output, for each word in the input string.

Example:

Line: a robot is cool
No robots
Little Robot
No robots
No robots
Andy
  • 49,085
  • 60
  • 166
  • 233
0

The question of how to do a case-insensitive string comparison in Python has been asked here on Stack Overflow before.

Assuming that you determine the correct method to do a case-sensitive comparison (CS) and a case-insensitive comparison (CI), then you probably want to rewrite your algorithm as follows (pseudocode, not Python code):

anyRobotSeen = false
for rs in r:
  if (CI(rs, "robot")) then:
    anyRobotSeen = true
    if (CS(rs, "robot")) then print "small robot"
    elif (CS(rs, "ROBOT")) then print "big robot"
    else print "medium robot"
if (anyRobotSeen == false) then print "no robots on this line"

Your current algorithm, if the comparison statements were fixed, would be printing "There are no robots in the line" for every single word that doesn't match "robot" in any case, even if one or more words on the line did match the target.

Community
  • 1
  • 1
afeldspar
  • 1,343
  • 1
  • 11
  • 26
0

Another problem:

rs[0] == "r" or "R" and rs[1] == "o" or "O" and rs[2] == "b" or "B" and rs[3] == "O" or"o" and rs[-1] == "T" or "t"

translates to

(rs[0] == "r")
or 
"R" 
and 
(rs[1] == "o")
or 
"O"  # etc.

which is always True because "R" evaluates to a true value. The correct syntax for what you're trying to do is

rs[0] in ("r", "R") and rs[1] in ("o", "O") and ...

although it would be much easier to just do

rs.lower() == "robot"
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
0

You may want to do an index find. That way you aren't searching for equality multiple times.

rl = ["robot", "ROBOT"]
r = input()
idx = rl.index(r)
if idx == 0:
    robot = "little"
elif idx == 1:
    robot = "big"
...

It just might save you readability by reducing the number of if statements. You could also create some enum class if you really wanted to, but that should only be done if you check for this multiple times.

Since you know all of the possible results you can easily create a master list.

ROBOT = "robot"
rl = [ROBOT, ROBOT.title(), ROBOT.capitalize()] # Look at .casefold() too
justengel
  • 6,132
  • 4
  • 26
  • 42