Advertisement
Guest User

boris - homework5

a guest
Jul 24th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.80 KB | None | 0 0
  1. from random import shuffle
  2. from copy import copy
  3.  
  4. def points_earned(question):
  5.   print(question['question'])
  6.   if len(question['correct_answers']) > 1:
  7.     print('(There are {0} correct answers)'.format(len(question['correct_answers'])))
  8.  
  9.   answers = copy(question['answers'])
  10.   shuffle(answers)
  11.   for index, answer in enumerate(answers):
  12.     print('{0}. {1}'.format(index+1, answer))
  13.    
  14.   print('Choose the correct answer(s) by their number:')
  15.   choices = []
  16.   for _ in range(len(question['correct_answers'])):
  17.     choice = int(input())
  18.     choices.append(choice)
  19.   print()
  20.    
  21.   points = 0
  22.   for choice in choices:
  23.     if answers[choice-1] in question['correct_answers']:
  24.       points += question['points'] / len(question['correct_answers'])
  25.      
  26.   return int(points)
  27.  
  28. def do_quiz(quiz):
  29.   if len(quiz) == 0:
  30.     print('There are no questions added yet.')
  31.     return
  32.   points = 0
  33.   allPoints = 0
  34.   for index, question in enumerate(quiz):
  35.     print("Question {0}: ".format(index+1), end='')
  36.     points += points_earned(question)
  37.     allPoints += question['points']
  38.    
  39.   print("You got {0} from {1} points".format(points, allPoints))
  40.   print("This is around {0}%".format((int)(points / allPoints * 100)))
  41.  
  42. def add_question_option(quiz):
  43.   new_question_itself = input("Enter the new question's question: ")
  44.  
  45.   print('Enter possible answers.\nWhen you are ready, type "done"')
  46.   answers = []
  47.   user_input = input()
  48.   while user_input != 'done':
  49.     answers.append(user_input)
  50.     user_input = input()
  51.  
  52.   print('Which answers are correct? Enter their numbers and when you are ready, type "done"')
  53.   correct_answers = []
  54.   user_input = input()
  55.   while user_input != 'done':
  56.     correct_answers.append(answers[int(user_input) - 1])
  57.     user_input = input()
  58.  
  59.   print('How many points will all correct answers be: ')
  60.   points = int(input())
  61.  
  62.   new_question = {
  63.   'question': new_question_itself,
  64.   'answers': answers,
  65.   'correct_answers': correct_answers,
  66.   'points': points
  67.   }
  68.  
  69.   quiz.append(new_question)
  70.  
  71. def remove_question_option(quiz):
  72.   for index, question in enumerate(quiz):
  73.     print('{0} . {1}'.format(index+1, question['question']))
  74.   print('\nChoose the question you want to remove.\nType its number')
  75.   choice = int(input()) - 1
  76.   quiz.pop(choice)
  77.   print('Question removed.')
  78.  
  79. def edit_question_option(quiz):
  80.   for index, question in enumerate(quiz):
  81.     print('{0} . {1}'.format(index+1, question['question']))
  82.   print('\nChoose the question you want to edit.\nType its number')
  83.   question_index = int(input()) - 1
  84.   question = quiz[question_index]
  85.  
  86.   print('Do you want to edit:\n1.the question itself\n2.something in the answers')
  87.   choice = input()
  88.  
  89.   if choice == '1':
  90.     new_question = input('Enter a new question: ')
  91.     question['question'] = new_question
  92.     print('New question added.')
  93.   else:
  94.     print('Do you want to:\n1.Remove an answer\n2.Add a new answer\n3.Edit an answer\n4.Define new correct answers')
  95.     choice = input()
  96.     if choice == '1':
  97.       for index, answer in enumerate(question['answers']):
  98.         print('{0} . {1}'.format(index+1, answer))
  99.       print('Choose the answer you want to remove.\nType its number')
  100.       answer_index = int(input()) - 1
  101.       question['answers'].pop(answer_index)
  102.       print('Answer removed.')
  103.     elif choice == '2':
  104.       new_answer = input('Type a new answer: ')
  105.       question['answers'].append(new_answer)
  106.       print('New answer added.')
  107.     elif choice == '3':
  108.       for index, answer in enumerate(question['answers']):
  109.         print('{0} . {1}'.format(index+1, answer))
  110.       print('Choose the answer you want to edit.\nType its number')
  111.       answer_index = int(input()) - 1
  112.       new_answer = input('Enter its new value: ')
  113.       question['answers'][answer_index] = new_answer
  114.       print('Question edited.')
  115.     else:
  116.       for index, answer in enumerate(question['answers']):
  117.         print('{0} . {1}'.format(index+1, answer))
  118.       print('Type the numbes of the correct answers\nWhen you are ready type "done"')
  119.       new_correct_answers = []
  120.       user_input = input()
  121.       while user_input != 'done':
  122.         new_correct_answers.append(question['answers'][int(user_input)-1])
  123.         user_input = input()
  124.       question['correct_answers'] = new_correct_answers
  125.       print('Correct answers defined.')
  126.      
  127. def main_menu(quiz):
  128.   print('Do you want to:\n1.Do the quiz\n2.Edit the quiz')
  129.   choice = input()
  130.   if choice == '1':
  131.     do_quiz(quiz)
  132.   else:
  133.     print('Do you want to:\n1.Edit a question\n2.Add a new question\n3.Remove a question')
  134.     choice = input()
  135.     if choice == '1':
  136.       edit_question_option(quiz)
  137.     elif choice == '2':
  138.       add_question_option(quiz)
  139.     else:
  140.       remove_question_option(quiz)
  141.      
  142. quiz = []
  143. while True:
  144.   main_menu(quiz)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement