# Sample context and question context = """Saudi Arabia, officially known as the Kingdom of Saudi Arabia, is the largest country in the Middle East. It is bordered by Jordan, Iraq, Kuwait, Bahrain, Qatar, the United Arab Emirates, Oman, and Yemen. The capital city is Riyadh. Saudi Arabia is known for its vast deserts, including the Rub’ al Khali, also known as the Empty Quarter, which is the largest continuous sand desert in the world. The country is also famous for being the birthplace of Islam and home to its two holiest cities, Mecca and Medina. Saudi Arabia's economy is heavily dependent on oil, making it one of the world’s leading oil producers and exporters. The official language is Arabic, and the currency is the Saudi riyal.""" question = "What is the capital of Saudi Arabia?" # Function to extract the answer from the context def find_answer(context, question): if "capital" in question.lower(): # Searching for the word "capital" in the context sentences = context.split(".") for sentence in sentences: if "capital" in sentence.lower(): # Assuming the answer follows after the word "capital" words = sentence.split() if "capital" in words: index = words.index("capital") # Return the next word which should be the capital city return words[index + 3] # The word after 'capital city is' answer = find_answer(context, question) print(f"Answer: {answer}")