0

So I am attempting to find letters within strings and perform actions based on whether it finds them. So far I have this:

name = easygui.enterbox("What is your name?");
term1 = 'i'
term2 = 'a'
position1 = name.find(term1)
position2 = name.find(term2)
if(position1 != -1 and position2 != -1 and position1 < position2):
    easygui.msgbox("Congratulations! You recieve a $3000 bonus.")
    bonus = True

...which works fine. My question is, how would I be able to revise it (as simply as possible please) so it finds the capital letters "I" and "A", and performs the same actions as "i" and "a" to lead to the bonus?

EDIT: I am attempting to search for both simultaneously (like combinations in the name that contain "ia", "Ia", "IA", or "iA"; the letters do not have to necessarily have to be next to each other, just in that particular order).

2 Answers2

1

You can convert the name content to lowercase: name = name.lower() and guarantee it always will contain ONLY lowercase letters.

You can check if the string contains the letters such:

if term1 in name and term2 in name and name.find(term1) < name.find(term2):
    ...
Chen A.
  • 10,140
  • 3
  • 42
  • 61
  • `name[term1] < name[term2]` I don't think it is possible to use string (or characters) as string indices. – ritiek Oct 10 '17 at 15:35
0

you cannot do find(term1 or upper(term1)) as its a logical "or" not find("term1 or uppercase term1").

If you just have to find if the character is present or not, you can do like this:

found=name.find(term1.upper())!=-1 or name.find(term1)!=-1

It will return True if either of the case is present.

SudipM
  • 416
  • 7
  • 14