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")
答案 0 :(得分:2)
您可以缩进其他语句,包括现有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")
答案 1 :(得分:2)
你的代码很清楚。据我所知,如果动物被拍了,你想问另一个问题。
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")
答案 2 :(得分:1)
缩进在python中很重要。要在另一个if语句中嵌套if语句,只需将其缩进到第一个下面的4个空格。
If ( var1 == 1 ):
If ( var2 == 2 ):
print "Both statements are true."