import random def is_valid_triangle(side1, side2, side3): return side1 + side2 > side3 and side1 + side3 > side2 and side2 + side3 > side1 rounds_number = 5 # seeing if the game is finished or not correct = 0 incorrect = 0 while correct + incorrect < rounds_number: # choosing the numbers for the shapes number1 = random.randint(2, 10) number2 = random.randint(2, 10) number3 = random.randint(2, 10) # choosing the shape shape = random.choice(['square', 'rectangle', 'triangle']) # choosing weather to use area or perimeter ap = random.choice(['area', 'perimeter']) # checking if the shape is a square or not if shape == 'square': player_answer = input(f'find the {ap} of a {shape} whose sides are {number1}cm long ') if not player_answer.isdigit(): print('enter a whole number try again on this new question') continue else: # calculating the perimeter of the square if ap == 'perimeter': answer = number1 * 4 # calculating the area of the square if ap == 'area': answer = number1 * number1 # checking if the shape is a rectangle or not elif shape == 'rectangle': player_answer = input(f'find the {ap} of a {shape} whose sides are {number1}cm and {number2}cm long ') if not player_answer.isdigit(): print('enter a whole number try again on this new question') continue else: # calculating the perimeter of the rectangle if ap == 'perimeter': answer = 2 * (number1 + number2) # calculating the area of the rectangle if ap == 'area': answer = number1 * number2 # checking if the shape is a triangle or not else: if not is_valid_triangle(number1, number2, number3): continue # calculating the perimeter of the triangle if ap == 'perimeter': player_answer = input( f'find the {ap} of a {shape} whose sides are {number1}cm, {number2}cm and {number3}cm long ') if not player_answer.isdigit(): print('enter a whole number try again on this new question') continue else: answer = number1 + number2 + number3 # calculating the area of the triangle if ap == 'area': player_answer = input(f'find the {ap} of a {shape} whose base is {number1}cm and height {number2}cm ') if not player_answer.isdigit(): print('enter a whole number try again on this new question') continue else: answer = number1 / 2 * number2 answer = round(answer) player_answer = int(player_answer) if player_answer == answer: print('correct') correct = correct + 1 else: print(f'incorrect the correct answer is {answer}') incorrect = incorrect + 1 print( f'Congratulations you finished all questions, you got {correct} correct and {incorrect} incorrect or {round(correct / rounds_number * 100)}% correct')