-3
about_pet = input("A sentence about your pet")
multi = "dog", "cat"
if all(multi) in about_pet:
    print ("wow, you have one more pets!", " Thanking you reading my 
story:) ")
elif "dog" in about_pet:
    print ("Ah, a dog.", " Thanking you reading my story:) ")
elif "cat" in about_pet:
    print ("Oh, a cat.", " Thanking you reading my story:) ")

i don't know how to search two strings in an input. The above captured is what I had done.

user8417321
  • 61
  • 1
  • 1
  • 2

2 Answers2

1

All works on an iterable, it returns true if all items within the iterable are true.

multi = ["dog", "cat"]
if all(m in about_pet for m in multi):
    print ("wow, you have one more pets!", " Thanking you reading my story:) ")
Simon Black
  • 913
  • 8
  • 20
0

you could put an if statement within an if statement so it checks if the sentence contains the word cat first then if it does it goes on check if it also contains the word dog to see if both words are in the string.

    about_pet = raw_input("A sentence about your pet")
    if "cat" in about_pet:
      if "dog"in about_pet:
        print "wow, you have one more pets! Thanking you reading my story:) "
    elif "dog" in about_pet:
        print "Ah, a dog. Thanking you reading my story:) "
    elif "cat" in about_pet:
        print "Oh, a cat. Thanking you reading my story:) " 

I know this might not be the cleverest solution but it is quite easy to understand. To simplify it you can check them one after another using an and

    if "cat" in about_pet and "dog" in about_pet:

or:

    if "cat" and "dog" in about_pet:

If you are using Python 2.7.10 ,you also need to use raw_input instead of input as in Python 2.7.10 input only accepts integers and float. And if you're using python 2.7.10(as i am) i don't do my print in brackets as it prints out the brackets sometimes. Also i have a suggestion to add code for when the user says they don't have a pet using an else at the end. Plus you don't have to put sentences in separate speech marks. I hope this helped!

yt.
  • 327
  • 1
  • 2
  • 11
  • The two ifs can be replaced with an and. if "cat" in about_pet and "dog"in about_pet – Simon Black Aug 04 '17 at 13:24
  • @Simon Black Okay, thanks so would it be`if "cat" in about_pet and "dog" in about_pet"` or can it be `if "cat" and "dog" in about_pet` ? – yt. Aug 04 '17 at 13:27