Advertisement
Guest User

math quiz

a guest
Mar 29th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.87 KB | None | 0 0
  1. def display_intro():
  2.     title = "** A Simple Math Quiz **"
  3.     print("*" * len(title))
  4.     print(title)
  5.     print("*" * len(title))
  6.  
  7.  
  8. def display_menu():
  9.     menu_list = ["1. Addition", "2. Subtraction", "3. Multiplication", "4. Integer Division", "5. Exit"]
  10.     print(menu_list[0])
  11.     print(menu_list[1])
  12.     print(menu_list[2])
  13.     print(menu_list[3])
  14.     print(menu_list[4])
  15.  
  16.  
  17. def display_separator():
  18.     print("-" * 24)
  19.  
  20.  
  21. def get_user_input():
  22.     user_input = int(input("Enter your choice: "))
  23.     while user_input > 5 or user_input <= 0:
  24.         print("Invalid menu option.")
  25.         user_input = int(input("Please try again: "))
  26.     else:
  27.         return user_input
  28.  
  29.  
  30. def get_user_solution(problem):
  31.     print("Enter your answer")
  32.     print(problem, end="")
  33.     result = int(input(" = "))
  34.     return result
  35.  
  36.  
  37. def check_solution(user_solution, solution, count):
  38.     if user_solution == solution:
  39.         count = count + 1
  40.         print("Correct.")
  41.         return count
  42.     else:
  43.         print("Incorrect.")
  44.         return count
  45.  
  46.  
  47. def menu_option(index, count):
  48.     number_one = randint(1, 21)
  49.     number_two = randint(1, 21)
  50.     if index is 1:
  51.         problem = str(number_one) + " + " + str(number_two)
  52.         solution = number_one + number_two
  53.         user_solution = get_user_solution(problem)
  54.         count = check_solution(user_solution, solution, count)
  55.         return count
  56.     elif index is 2:
  57.         problem = str(number_one) + " - " + str(number_two)
  58.         solution = number_one - number_two
  59.         user_solution = get_user_solution(problem)
  60.         count = check_solution(user_solution, solution, count)
  61.         return count
  62.     elif index is 3:
  63.         problem = str(number_one) + " * " + str(number_two)
  64.         solution = number_one * number_two
  65.         user_solution = get_user_solution(problem)
  66.         count = check_solution(user_solution, solution, count)
  67.         return count
  68.     else:
  69.         problem = str(number_one) + " // " + str(number_two)
  70.         solution = number_one // number_two
  71.         user_solution = get_user_solution(problem)
  72.         count = check_solution(user_solution, solution, count)
  73.         return count
  74.  
  75.  
  76. def display_result(total, correct):
  77.     if total > 0:
  78.         result = correct / total
  79.         percentage = round((result * 100), 2)
  80.     if total == 0:
  81.         percentage = 0
  82.     print("You answered", total, "questions with", correct, "correct.")
  83.     print("Your score is ", percentage, "%. Thank you.", sep = "")
  84.  
  85.  
  86. def main():
  87.     display_intro()
  88.     display_menu()
  89.     display_separator()
  90.  
  91.     option = get_user_input()
  92.     total = 0
  93.     correct = 0
  94.     while option != 5:
  95.         total = total + 1
  96.         correct = menu_option(option, correct)
  97.         option = get_user_input()
  98.  
  99.     print("Exit the quiz.")
  100.     display_separator()
  101.     display_result(total, correct)
  102.  
  103. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement