Read file as list of tuples and display in answer format

时间:2017-04-24 17:17:36

标签: python tuples

I have to read a text file (notepad txt) which is in the format of:

Why are manhole covers round in shape?

The cover cannot fall through if it is circular._Circular covers do not need to be precisely aligned._A round cover is easy to move and roll._Human beings have a roughly circular cross section._All of the above.

5

What is the result of dividing 3 by 4?

1.33333...._0.75._12._7._7._0.7777777777777

2

In the series 1,2,3,4,5,...100, how many of these numbers have the digit 7 in them?

9._10._19._20._11

3

The answers are separated by (._) and the answer is the number that follows the question as it coresponds to the answer choices.

I want to have each set of questions stored as a tuple in a list. And I want to display on screen in the format as shown below.

1.Question
 1)First option  
 2)Second option  
 3)Third option
  #etc as there are more answers for some of the questions

2.  Question
 1) First option
 2) Second option
 3) Third option
 #etc as there are more answers for some of the questions

How do I write code to do so? Thanks for the help!

1 个答案:

答案 0 :(得分:1)

To pull this off you will need to do a couple steps.

  1. Read in the file
  2. Convert the data
    • You will need to treat the lines in groups of threes. The eaisest way to do this is to track the line number and do different things based on index % 3
      • index % 3 = 0
        • Store the question in a temporary variable
      • index % 3 = 1
      • index % 3 = 2
        • store the question and answers in the python tuple
  3. Now that you have the tuple display it using standard output mechanisms