answer.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import csv
  5. from datetime import datetime
  6. from pathlib import Path
  7. def answer_one(question: str, options: list[str]) -> int:
  8. """
  9. 每一个问题有四个选项, 返回选项的序号(1、2、3)
  10. :param question:
  11. :param options:
  12. :return:
  13. """
  14. file = str(Path(__file__).parent / 'data.csv')
  15. with open(file, newline='', encoding='utf-8-sig') as csvfile:
  16. reader = csv.reader(csvfile)
  17. next(reader)
  18. for row in reader:
  19. if row[0] == question:
  20. try:
  21. return options.index(row[1]) + 1
  22. except ValueError:
  23. return 1
  24. return 1
  25. if __name__ == "__main__":
  26. question = '以下式神中,手持折扇的是'
  27. options = ['生命上限', '鬼王', '荒川之主']
  28. start_time = datetime.now()
  29. print(answer_one(question, options))
  30. print(datetime.now() - start_time)
  31. print(answer_one(question, options))