Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import csv
- import os
- def clear_screen():
- os.system('cls' if os.name == 'nt' else 'clear')
- questions = []
- # Read questions from the CSV file
- with open('actual_tests_1-100.csv', newline='') as csvfile:
- reader = csv.reader(csvfile)
- next(reader) # Skip the header row
- for row in reader:
- question = {
- 'question': row[0],
- 'options': row[1:5],
- 'answer': row[5]
- }
- questions.append(question)
- random.shuffle(questions)
- score = 0
- for i, question in enumerate(questions):
- clear_screen()
- print(f'QUESTION NO: {i+1}')
- print(question['question'])
- for option in question['options']:
- print(option)
- user_answer = input('Answer: ').upper() # Convert user's answer to uppercase
- if user_answer == question['answer'].upper(): # Compare answers in uppercase
- print('Correct!')
- score += 1
- else:
- print('Incorrect!')
- reveal_answer = input('Do you want to reveal the correct answer? (Y/N): ').upper()
- if reveal_answer == 'Y':
- print(f'The correct answer is: {question["answer"]}')
- if i < len(questions) - 1:
- input('Press Enter to continue...')
- print('\nQuiz completed!')
- print(f'Your score: {score}/{len(questions)}')
Advertisement
Add Comment
Please, Sign In to add comment