Advertisement
DrAungWinHtut

exam_list.py

Mar 10th, 2024
572
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.00 KB | None | 0 0
  1. #question.txt
  2. #=============
  3. # Q:The longest river in the world? * 1. Nile, 2. Maykha, 3. Malikha * 1
  4. # Q:The highest mountain in the world? * 1. Everest, 2. Fuji , 3. Kharkaborazi * 1
  5. # Q:The longest river in the world? * 1. Nile, 2. Maykha, 3. Malikha * 1
  6. # Q:The highest mountain in the world? * 1. Everest, 2. Fuji , 3. Kharkaborazi * 1
  7. # Q:The longest river in the world? * 1. Nile, 2. Maykha, 3. Malikha * 1
  8. # Q:The highest mountain in the world? * 1. Everest, 2. Fuji , 3. Kharkaborazi * 1
  9. # Q:The longest river in the world? * 1. Nile, 2. Maykha, 3. Malikha * 1
  10. # Q:The highest mountain in the world? * 1. Everest, 2. Fuji , 3. Kharkaborazi * 1
  11. # Q:The longest river in the world? * 1. Nile, 2. Maykha, 3. Malikha * 1
  12. # Q:The highest mountain in the world? * 1. Everest, 2. Fuji , 3. Kharkaborazi * 1
  13.  
  14.  
  15.  
  16. # pip install icecream
  17. from icecream import ic   #ic is used for detailed printing
  18.  
  19. def quiz():
  20.     marks = 0
  21.     question = [] #empty list to store one question
  22.     questions = [] #empty list to store all questions which concat with '*'
  23.  
  24.     #collect all questions from file question.txt
  25.     input_file = open('question.txt','r')
  26.     questions = input_file.readlines()    # questions - List , one item contains q, options, correct ans, concat with '*'
  27.     input_file.close()
  28.    
  29.     #print(questions)
  30.  
  31.     for q in questions:
  32.         # q is one item of questioncontains q, options, correct ans, concat with '*'
  33.         question = q.split('*')  # question list contains [0]Q, [1]options, [2]correct ans
  34.         correct_ans = question[2].strip() #strip is used to eliminate unwanted space or '\n' '\r' etc..
  35.         print(question[0])
  36.         print(question[1])
  37.         user_ans = input('Choose correct answer: ')
  38.         #ic(user_ans)
  39.         #ic(correct_ans)
  40.  
  41.         if user_ans == correct_ans:
  42.             print('Correct')
  43.             marks += 1
  44.         else:
  45.             print('Wrong ans')
  46.        
  47.         print(f'Total marks = {marks}')
  48.  
  49.  
  50. if __name__ == "__main__":
  51.     quiz()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement