File size: 1,028 Bytes
ba3f08a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | !pip install wikipedia
import wikipedia
wikipedia.set_lang("en")
def science_bot(user_input):
topic = user_input.lower().strip()
for prefix in ["what is", "define", "explain", "tell me about"]:
if topic.startswith(prefix):
topic = topic.replace(prefix, "").strip()
try:
return wikipedia.summary(topic, sentences=3)
except wikipedia.exceptions.DisambiguationError as e:
return f"⚠️ That topic is ambiguous. Try one of these:\n{', '.join(e.options[:5])}"
except wikipedia.exceptions.PageError:
try:
return wikipedia.summary(topic.title(), sentences=3)
except:
try:
return wikipedia.summary(f"{topic} (physics)", sentences=3)
except:
return f"❌ I couldn't find anything on '{topic}'. Try a different science topic."
while True:
user_input = input("Ask a science question (or type 'exit'): ")
if user_input.lower() == "exit":
break
print(science_bot(user_input)) |