PROGRAM:
import random # for executing while loop indefinitely flag = True # count the number of incorrect attempts in a row count = 0 while flag: # Generate 2-digit random numbers number1 = random.randint(0, 99) number2 = random.randint(0, 99) if count < 2: # prompt the user to enter an answer and convert it to integer answer = int(input("What is " + str(number1) + " + " + str(number2) + " ? ")) # check if it is equal to -1 or not if answer != -1: # check if answer is correct if number1 + number2 == answer: # resetting the counter count = 0 # Display result print(number1, "+", number2, "=", answer, "is True") # if answer is incorrect else: # increment counter by 1 count = count + 1 # Display result print(number1, "+", number2, "=", answer, "is False") # if answer = -1 is given else: flag = False print("Answer = -1 given, exiting the program!") # if counter is equal to 2 else: flag = False print("Answered incorrectly 2 questions in a row, exiting the program!")
OUTPUT:
case1 : When -1 is given on 1st try
What is 39 + 62 ? -1
Answer = -1 given, exiting the program!
case2 : When an incorrect answer is given 2 times in a row
What is 72 + 82 ? 154
72 + 82 = 154 is True
What is 65 + 56 ? 121
65 + 56 = 121 is True
What is 18 + 36 ? 35
18 + 36 = 35 is False
What is 87 + 79 ? 62
87 + 79 = 62 is False
Answered incorrectly 2 questions in a row, exiting the program!
case3 : When -1 is given after some correct answers
What is 43 + 56 ? 99
43 + 56 = 99 is True
What is 85 + 47 ? 132
85 + 47 = 132 is True
What is 69 + 33 ? 102
69 + 33 = 102 is True
What is 96 + 21 ? 100
96 + 21 = 100 is False
What is 66 + 8 ? -1
Answer = -1 given, exiting the program!
case4 : When the user inputs incorrect answers in between
What is 92 + 15 ? 107
92 + 15 = 107 is True
What is 84 + 5 ? 89
84 + 5 = 89 is True
What is 66 + 61 ? 65
66 + 61 = 65 is False
What is 19 + 37 ? 56
19 + 37 = 56 is True
What is 50 + 17 ? 67
50 + 17 = 67 is True
What is 28 + 91 ? 119
28 + 91 = 119 is True
What is 39 + 93 ? 132
39 + 93 = 132 is True
What is 99 + 64 ? 21
99 + 64 = 21 is False
What is 18 + 8 ? 8745
18 + 8 = 8745 is False
Answered incorrectly 2 questions in a row, exiting the
program!
SCREENSHOTS:
Case 1:
Case 2:
Case 3:
Case 4:
Hope this helps!
Original Question Problem: Game: learn addition) Write a program that generates two integers under 100 and prompts the...