pranaman

Untitled

Jul 6th, 2023
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | Source Code | 0 0
  1. import random
  2. import csv
  3. import os
  4.  
  5. def clear_screen():
  6. os.system('cls' if os.name == 'nt' else 'clear')
  7.  
  8. questions = []
  9.  
  10. # Read questions from the CSV file
  11. with open('actual_tests_1-100.csv', newline='') as csvfile:
  12. reader = csv.reader(csvfile)
  13. next(reader) # Skip the header row
  14. for row in reader:
  15. question = {
  16. 'question': row[0],
  17. 'options': row[1:5],
  18. 'answer': row[5]
  19. }
  20. questions.append(question)
  21.  
  22. random.shuffle(questions)
  23.  
  24. score = 0
  25.  
  26. for i, question in enumerate(questions):
  27. clear_screen()
  28. print(f'QUESTION NO: {i+1}')
  29. print(question['question'])
  30. for option in question['options']:
  31. print(option)
  32.  
  33. user_answer = input('Answer: ').upper() # Convert user's answer to uppercase
  34.  
  35. if user_answer == question['answer'].upper(): # Compare answers in uppercase
  36. print('Correct!')
  37. score += 1
  38. else:
  39. print('Incorrect!')
  40. reveal_answer = input('Do you want to reveal the correct answer? (Y/N): ').upper()
  41. if reveal_answer == 'Y':
  42. print(f'The correct answer is: {question["answer"]}')
  43.  
  44. if i < len(questions) - 1:
  45. input('Press Enter to continue...')
  46.  
  47. print('\nQuiz completed!')
  48. print(f'Your score: {score}/{len(questions)}')
Advertisement
Add Comment
Please, Sign In to add comment