使用“else”

时间:2017-03-11 08:51:54

标签: python python-3.x

无法弄清楚我做错了什么。如果有人使用维基百科列表,维基百科的if将与其他人一起被删除。

wikipedia=["search","infromation"]
weatherfor=["show weather","weather today","weather forecast","weather"]
action=(input("What would you want me to do : "))
if any(word in action for word in wikipedia):
  print("Here Is What I Found On Wikipedia.")
if any(word in action for word in weatherfor):
  print("The Weather For Today Is")
else:
  print ("Sorry I Haven't learnt this yet")

1 个答案:

答案 0 :(得分:2)

您已经过度使用了if else,请尝试以下简单结构:

wikipedia=["search", "infromation"]
weatherfor=["show weather", "weather today", "weather forecast", "weather"]

action=(input("What would you want me to do : "))

if action in wikipedia:
  print("Here Is What I Found On Wikipedia.")
elif aaction in weatherfor:
  print("The Weather For Today Is")
else:
  print ("Sorry I Haven't learnt this yet")

in可以在任何可迭代中搜索给定值(在这种情况下为list