Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import shuffle
- from copy import copy
- def points_earned(question):
- print(question['question'])
- if len(question['correct_answers']) > 1:
- print('(There are {0} correct answers)'.format(len(question['correct_answers'])))
- answers = copy(question['answers'])
- shuffle(answers)
- for index, answer in enumerate(answers):
- print('{0}. {1}'.format(index+1, answer))
- print('Choose the correct answer(s) by their number:')
- choices = []
- for _ in range(len(question['correct_answers'])):
- choice = int(input())
- choices.append(choice)
- print()
- points = 0
- for choice in choices:
- if answers[choice-1] in question['correct_answers']:
- points += question['points'] / len(question['correct_answers'])
- return int(points)
- def do_quiz(quiz):
- if len(quiz) == 0:
- print('There are no questions added yet.')
- return
- points = 0
- allPoints = 0
- for index, question in enumerate(quiz):
- print("Question {0}: ".format(index+1), end='')
- points += points_earned(question)
- allPoints += question['points']
- print("You got {0} from {1} points".format(points, allPoints))
- print("This is around {0}%".format((int)(points / allPoints * 100)))
- def add_question_option(quiz):
- new_question_itself = input("Enter the new question's question: ")
- print('Enter possible answers.\nWhen you are ready, type "done"')
- answers = []
- user_input = input()
- while user_input != 'done':
- answers.append(user_input)
- user_input = input()
- print('Which answers are correct? Enter their numbers and when you are ready, type "done"')
- correct_answers = []
- user_input = input()
- while user_input != 'done':
- correct_answers.append(answers[int(user_input) - 1])
- user_input = input()
- print('How many points will all correct answers be: ')
- points = int(input())
- new_question = {
- 'question': new_question_itself,
- 'answers': answers,
- 'correct_answers': correct_answers,
- 'points': points
- }
- quiz.append(new_question)
- def remove_question_option(quiz):
- for index, question in enumerate(quiz):
- print('{0} . {1}'.format(index+1, question['question']))
- print('\nChoose the question you want to remove.\nType its number')
- choice = int(input()) - 1
- quiz.pop(choice)
- print('Question removed.')
- def edit_question_option(quiz):
- for index, question in enumerate(quiz):
- print('{0} . {1}'.format(index+1, question['question']))
- print('\nChoose the question you want to edit.\nType its number')
- question_index = int(input()) - 1
- question = quiz[question_index]
- print('Do you want to edit:\n1.the question itself\n2.something in the answers')
- choice = input()
- if choice == '1':
- new_question = input('Enter a new question: ')
- question['question'] = new_question
- print('New question added.')
- else:
- print('Do you want to:\n1.Remove an answer\n2.Add a new answer\n3.Edit an answer\n4.Define new correct answers')
- choice = input()
- if choice == '1':
- for index, answer in enumerate(question['answers']):
- print('{0} . {1}'.format(index+1, answer))
- print('Choose the answer you want to remove.\nType its number')
- answer_index = int(input()) - 1
- question['answers'].pop(answer_index)
- print('Answer removed.')
- elif choice == '2':
- new_answer = input('Type a new answer: ')
- question['answers'].append(new_answer)
- print('New answer added.')
- elif choice == '3':
- for index, answer in enumerate(question['answers']):
- print('{0} . {1}'.format(index+1, answer))
- print('Choose the answer you want to edit.\nType its number')
- answer_index = int(input()) - 1
- new_answer = input('Enter its new value: ')
- question['answers'][answer_index] = new_answer
- print('Question edited.')
- else:
- for index, answer in enumerate(question['answers']):
- print('{0} . {1}'.format(index+1, answer))
- print('Type the numbes of the correct answers\nWhen you are ready type "done"')
- new_correct_answers = []
- user_input = input()
- while user_input != 'done':
- new_correct_answers.append(question['answers'][int(user_input)-1])
- user_input = input()
- question['correct_answers'] = new_correct_answers
- print('Correct answers defined.')
- def main_menu(quiz):
- print('Do you want to:\n1.Do the quiz\n2.Edit the quiz')
- choice = input()
- if choice == '1':
- do_quiz(quiz)
- else:
- print('Do you want to:\n1.Edit a question\n2.Add a new question\n3.Remove a question')
- choice = input()
- if choice == '1':
- edit_question_option(quiz)
- elif choice == '2':
- add_question_option(quiz)
- else:
- remove_question_option(quiz)
- quiz = []
- while True:
- main_menu(quiz)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement