Python 使用';如果';但是没有';其他';要做到这一点

Python 使用';如果';但是没有';其他';要做到这一点,python,if-statement,Python,If Statement,又是新手了。我试图完成以下问题,但仍停留在粗体字的条件上: 在变量中获取用户输入:about\u pet 使用一系列的if语句以适当的对话进行响应 检查“狗”是否在关于宠物的字符串中(示例回复“啊,一只狗”) 检查关于宠物的字符串中是否有“猫” 检查是否有一只或多只动物与宠物有关 不需要别人的 以感谢这个故事结束 我写的代码: about_pet = input("Enter a sentence about a pet: ") if 'dog' in about_pet.lower():

又是新手了。我试图完成以下问题,但仍停留在粗体字的条件上:

  • 在变量中获取用户输入:
    about\u pet
  • 使用一系列的if语句以适当的对话进行响应
  • 检查“狗”是否在关于宠物的字符串中(示例回复“啊,一只狗”)
  • 检查关于宠物的字符串中是否有“猫”
  • 检查是否有一只或多只动物与宠物有关
  • 不需要别人的
  • 以感谢这个故事结束
我写的代码:

about_pet = input("Enter a sentence about a pet: ")

if 'dog' in about_pet.lower():
    print ("Ah, a dog")
if 'cat' in about_pet.lower():
    print ("Ah, a cat")
elif 'dog, cat' in about_pet.lower():
      print ("Ah, there is one or more pet")

print("Thank you for your story")
我尝试了一些其他的方法,但还是被卡住了。你能帮我解决这个问题吗

提前谢谢你

谢谢大家的帮助!我翻开了课程论坛,在我看来,讲师正在使用布尔值。i、 e about_pet.lower()中的“dog”==True,用于验证输入语句是否有多个pet。我被困在这里如何利用布尔来检查输入语句

再次感谢大家的帮助

if 'dog' in about_pet.lower():
print ("Ah, a dog")
if 'cat' in about_pet.lower():
    print ("Ah, a cat")
if 'dog' in about_pet.lower() or 'cat' in about_pet.lower():
      print ("Ah, there is one or more pets")
将上述条件更改为下述条件:

if 'dog' in about_pet.lower() or 'cat' in about_pet.lower():
    print ("Ah, there is more than one pet")

这个使用2个标志和条件优化的简单逻辑如何(好的,有一个
else
,但它只保存了一些测试,没有
else
的编码是一个不合理的约束):

请注意,单词边界正则表达式最好避免单词中的匹配(如“caterpillar”):


其余的测试保持不变

在宠物数量超过1只的情况下,列出一个列表是很有用的

def pet_convo():
    about_pet=input('What pets do you have? ')
    myList = about_pet.split(',')
    if len(myList)>1:
        print('Ah, you have many animals')
    elif 'cat' in about_pet.lower():
        print("Ah, a cat")
    elif 'dog' in about_pet.lower():
        print('Ah, a dog')
    print('Thank you for your story')

记住正确的位置;)


并不是说如果句子中有一只猫,你就永远不会进入elif条件。请尝试
all(在about_pet中的x代表在['dog','cat']中的x)
之前,你应该包括“if('dog'在about_pet.lower())和about_pet.lower()中的('cat'):”语句。并更正身份。不,我是说“或”。问题是1或更多。我复制了海报的原始措辞。我不关心措辞。是的,从技术上讲,“有不止一只宠物”这句话是不准确的。看看“如果有一只或更多的动物”这句话,我认为它不应该是,而不是或。我不明白,为什么”和“@Diego你能详细说明这句话的原因吗?”。但是,如果ideia要验证是否至少有一种动物在名单上,那么你是正确的。我收回我的反对意见:要求与你一致,但OP的打印不一致。if语句的重新排序意味着,如果发生这种情况,你将降落在about_pet branch的“猫”和“狗”上,并且不要意外进入只有“猫”或“狗”的情况“狗”是关于宠物的
about_pet = about_pet.lower()
has_dog = 'dog' in about_pet
has_cat = 'cat' in about_pet

if has_dog:
    if has_cat:
        print ("Ah, there is more than one pet")
    else:
        print ("Ah, a dog")
elif has_cat:
    print ("Ah, a cat")
import re

has_dog = re.search(r'\bdog\b',about_pet,re.IGNORECASE) # note: raw prefix before \b word boundary!
has_cat = re.search(r'\bcat\b',about_pet,re.IGNORECASE)
def pet_convo():
    about_pet=input('What pets do you have? ')
    myList = about_pet.split(',')
    if len(myList)>1:
        print('Ah, you have many animals')
    elif 'cat' in about_pet.lower():
        print("Ah, a cat")
    elif 'dog' in about_pet.lower():
        print('Ah, a dog')
    print('Thank you for your story')
about_pet = input("Enter a sentence about a pet: ")
if "cat" in about_pet and "dog" in about_pet:
    print("Ah! there is one or more pet!")
elif "cat" in about_pet:
    print("Ah! a cat ")
elif "dog" in about_pet:
    print("Oh! a dog ")
print("Thank you for your story!")