Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Write the code in a different way, in a more simplified form

Describe the TrianglePS(a, P, S) procedure that calculates on the side a of an equilateral triangle its perimeter P = 3·a and area S = a2**√3/4 (a – input, P and S — output parameters are all
parameters). Using this procedure, find the perimeters and areas of three equilateral triangles with these sides.How else can I write.Thank you in advance
“ `

import math
def proc(a):
P = 3 * a
S =a**2*sqrt*3/4
return [P, S]


x = [1, 2, 3]
for elem in x:
  print(proc(elem)[0])
  print(proc(elem)[1])

`

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Bear in mind that a Python function can return a tuple – i.e., effectively more than one value. Therefore this could be greatly simplified as follows:

import random


def TrianglePS(a):
    return 3 * a, 3 ** 0.5 / 4 * a * a


for i in range(3):
    a = random.randrange(1, 10)
    print(f'a={a}')
    p, s = TrianglePS(a)
    print(f'P={p}')
    print(f'S={s}')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading