cosname

Python homework

Mar 30th, 2015
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. from sys import argv
  3.  
  4. def parse(line):
  5.  
  6. line_type = "question"
  7.  
  8. if line[0:2] == '# ':
  9. line_type = "heading"
  10. return (line_type, line)
  11.  
  12. if line[0:3] == '## ':
  13. line_type = "description"
  14. return (line_type, line)
  15.  
  16. # if line[0].isdigit() and line[1] == ')' and line[2] == ' ':
  17. if line[0].isdigit() and line[1:3] == ') ':
  18. line_type = "answer"
  19. line = {
  20. "number": int(line[0]),
  21. "line": line[3:]
  22. }
  23. return (line_type, line)
  24.  
  25. # if line[0] == '-' and line[1] == ' ':
  26. if line[0:2] == '- ':
  27. line_type = "right_answer"
  28. line = line[2:].split(',')
  29.  
  30. return (line_type, line)
  31.  
  32. script, file_name = argv
  33.  
  34. # f = open(file_name)
  35. # content = f.read()
  36. # content = content.splitlines()
  37.  
  38. content = open(file_name).read().splitlines()
  39.  
  40. question_list = []
  41. current = -1
  42.  
  43. for line in content:
  44. if line == "": continue
  45. (type_line, line) = parse(line)
  46.  
  47. if type_line == "question":
  48. question_list.append({
  49. "answers": []
  50. })
  51. current = current + 1
  52. question_list[current]["question"] = line
  53.  
  54. if type_line == "answer":
  55. question_list[current]["answers"].append(line)
  56.  
  57. if type_line == "right_answer":
  58. question_list[current]["right_answer"] = line
  59.  
  60. for question in question_list:
  61. print question
  62.  
  63. # Завдання:
  64. #######
  65. # Вивести питання за питанням.
  66. # Після кожного питання вивести варіанти відповіді
  67. # Після варіантів відповіді, запропонувати користувачу відповісти
  68. # Вивести на екарн консолі відповідь питання
  69. #######
  70. # Слід викориcтовувати конструкцію `for <item> in <item_list>:` (як для питань, так і для варіантів відповіді) та функцію raw_input(">")
  71. # Для кращого розуміння циклів, можете прочитати цю публікацію: http://jjc.freeshell.org/easytut/Easytut_Russian/node11.html
Advertisement
Add Comment
Please, Sign In to add comment