if语句中的if语句?

2024-09-19 23:27:09 发布

您现在位置:Python中文网/ 问答频道 /正文

FarmGround=input("Do you want to pat the animal? ") #this is the input
if FarmGround==("Yes") or FarmGround==("yes"):  #this is if
    print("You patted the animal")  #print statement if you patted the animal it will go onto the next if statement
if print=("You patted the animal"):
elif FarmGround==("No") or FarmGround==("no"): #this is elif
    print("You didn't patt the animal and it is triggered")

undescribed image


Tags: ortheyouinputifisitthis
3条回答

您可以缩进其他语句,包括现有if块中的if语句,就像缩进第一条print语句一样。你的问题不清楚你到底想做什么,所以我将填充一些伪代码(你可以用你真正想要的任何东西替换):

FarmGround=input("Do you want to pat the animal? ")
if FarmGround==("Yes") or FarmGround==("yes"):
    print("You patted the animal")
    some_other_answer = input("Some other question?")  # here's more code inside the first if
    if some_other_answer == "Foo":   # it can include another if statement, if you want it to
        print("Foo!")
elif FarmGround==("No") or FarmGround==("no"):
    print("You didn't patt the animal and it is triggered") 

你的密码很清楚。我的理解是你想问另一个问题,如果动物被拍。你知道吗

FarmGround=input("Do you want to pat the animal? ") #this is the input

if FarmGround=="Yes" or FarmGround=="yes":  #this is if
    print("You patted the animal") 

    holy_field = input("Did you clear the field?") 
    if holy_field.lower() == "yes":
         print("Do something else. Don't look at me.") 
    else: 
         print("When are you going to do it ?")          
elif FarmGround== "No" or FarmGround== "no": #this is elif
    print("You didn't patt the animal and it is triggered")

缩进在python中很重要。要在另一个if语句中嵌套if语句,只需在第一个if语句下面缩进4个空格即可。你知道吗

If ( var1 == 1 ):
    If ( var2 == 2 ):
        print "Both statements are true."

相关问题 更多 >