import random import sys def is_valid_triangle(side1, side2, side3): return side1 + side2 > side3 and side1 + side3 > side2 and side2 + side3 > side1 name=input('what is your name? ') print(f'\nWelcome to areas and perimeter unlimited {name}\n') #this code checks if the user wants the rules or not while True: rules=input('Do you want to know how to solve the area and perimeter of squares, rectangles and triangles?(yes/no) ').lower() if rules in ('yes', 'y'): print('\n ---- formulas ----') print('For Squares') print('Perimeter = 4 x length') print('area = length x length\n') print('For Rectangles') print('Perimeter = 2 x (width + length)') print('area = width x length\n') print('For Triangles') print('Perimeter = side a + side b + side c') print('area = 1⁄2 base x height') break elif rules in ['no','n']: print('\nI hope your good at math then.') break else: print('\ncome again? (yes/no)') continue while True: correct = 0 incorrect = 0 #asking the player how many questions they want rounds_number = input('\nHow many questions do you want?(pick a number from 5-20) ') #checking if the player entered a number grater then 5 but lesser than 20 if not rounds_number.isdigit() or not 5 <= int(rounds_number) <= 20: print('\nchoose a number between 5 and 20') continue else: rounds_number = int(rounds_number) #seeing if the game is finished or not 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'\nfind the {ap} of a {shape} whose sides are all {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'\nfind 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'\nfind 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'\nfind the {ap} of a {shape} whose base is {number1}cm and has a height of {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('\ncorrect') correct += 1 else: print(f'\nincorrect the correct answer is {answer}') incorrect += 1 print(f'\nCongratulations you finished all questions, you got {correct} correct and {incorrect} incorrect or {round(correct/rounds_number*100)}% correct') again = input('do you want to play again? (yes/no) ') if again in ('yes', 'y'): continue else: print(f'\nThank you for playing {name}') sys.exit()