Python 无法获取要执行的两条if语句和一条else语句
因此,目前我正在尝试制作一个个人好友程序。我希望它是两个if语句和一个其他语句。这两个if语句有不同的单词触发器,这就是为什么有两个。当我想做一个else语句时,问题就出现了,所以如果没有键入特定的单词触发器,它仍然会说些什么。这是密码Python 无法获取要执行的两条if语句和一条else语句,python,if-statement,Python,If Statement,因此,目前我正在尝试制作一个个人好友程序。我希望它是两个if语句和一个其他语句。这两个if语句有不同的单词触发器,这就是为什么有两个。当我想做一个else语句时,问题就出现了,所以如果没有键入特定的单词触发器,它仍然会说些什么。这是密码 sport = input("What sports do you play?\n") if sport in ['soccer','baseball','dance','basketball','golf','skiing','surfing']: p
sport = input("What sports do you play?\n")
if sport in ['soccer','baseball','dance','basketball','golf','skiing','surfing']:
print(sport, "sounds fun")
if sport in ['none','not at the moment','nope','none atm','natm']:
print("Im not really into sports either")
else:
print(sport, "is a sport?")
你可以看到else语句应该用“拇指摔跤是一项运动?”。相反,如果我说一项运动会引发“棒球听起来很有趣”“棒球是一项运动?”我不想让它同时引发这两个问题。我做错什么了吗?请帮忙
sport = input("What sports do you play?\n")
if sport in ['soccer','baseball','dance','basketball','golf','skiing','surfing']:
print(sport, "sounds fun")
elif sport in ['none','not at the moment','nope','none atm','natm']:
print("Im not really into sports either")
else:
print(sport, "is a sport?")
注意elif
而不是第二个if
。如果意味着在语句链中,只有一个语句将被执行,那么这表示else
注意elif
而不是第二个if
。这表示else if
意思是在语句链中,只执行一个语句。使用if elif
语句来区分两种以上的情况,而不是使用条件语句if else
:
if sport in ['soccer','baseball','dance','basketball','golf','skiing','surfing']:
print(sport, "sounds fun")
elif sport in ['none','not at the moment','nope','none atm','natm']:
print("I'm not really into sports either")
else:
print(sport, "is a sport?")
如果您打算添加另一个case,除了我在代码中改进的case之外,只需遵循If-elif-else
语句的语法模式:
if expression1:
statement(s)
elif expression2:
statement(s)
elif expression3: #You can add another line of elif if you add another case, here it is labeled expression3.
statement(s)
else:
statement(s)
使用if-elif-else
语句来区分两种以上的情况,而不是使用条件语句if-else
:
if sport in ['soccer','baseball','dance','basketball','golf','skiing','surfing']:
print(sport, "sounds fun")
elif sport in ['none','not at the moment','nope','none atm','natm']:
print("I'm not really into sports either")
else:
print(sport, "is a sport?")
如果您打算添加另一个case,除了我在代码中改进的case之外,只需遵循If-elif-else
语句的语法模式:
if expression1:
statement(s)
elif expression2:
statement(s)
elif expression3: #You can add another line of elif if you add another case, here it is labeled expression3.
statement(s)
else:
statement(s)