Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to judge the type of triangle

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to use Python to judge the type of triangle". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Judge the type of triangle according to the three sides of the input triangle, and output its area and type.

C code:

/ * on the seventh day, judge the type of triangle * / # include # include # include / / sqrt/* to judge whether a triangle can be formed or not: a triangle > c / b / a Helen's formula: √ [p (psquea) (psqueb) (psqueb) (pMEC)], where the type of triangles: equilateral triangles, isosceles triangles, right triangles, ordinary triangles * / void main (void) {int alight bdirection c / float Printf ("Please enter the size of the three sides of the triangle to be queried, for example: 2Please enter the size of the three sides of the triangle to be queried, for example: 2page2\ n"); scanf ("% drech% dpene% d", & & (a + b > c) & (b + c > a) & & (c + a > b)) {printf ("can form triangles\ n"); p = (a + b + c) / 2 Area = (float) sqrt (p * (p-a) * (p-b) * (p-c)); printf ("triangle area is% f\ n", area); if ((a = = b) & & (b = = c) printf ("the triangle you entered is equilateral triangle\ n") Else if ((a = = b) | | (b = = c) | | (c = = a) printf ("the triangle you entered is an isosceles triangle\ n"); else if ((a ^ 2 + b ^ 2 = = c ^ 2) | | (a ^ 2 + c ^ 2 = = b ^ 2) | printf ("the triangle you entered is a right triangle\ n"); elseprintf ("the triangle you entered is an ordinary triangle\ n");} elseprintf ("cannot form a triangle\ n") System ("pause");}

The results show that:

Python3.4.4 Code:

Import math as mside_lengths = input ('Please enter the size of the three sides of the triangle to be queried For example: 2Jing 2Jing 2\ n') length = len (side_lengths) flag = 0p = 0for i in range (0pl length): if side_ stories [I] = =' ': flag + = 1if flag = = 1: a = iif flag = = 2: B = ia_length = float (side_lengths [: a]) b_length = float (side_ cycles [a + 1iif flag]) c_length = float (side_ cycles [b + 1:]) if ((a_length + b_length > c_length) & (b_length + c_length > a_length)\ & (c_length + a) _ length > b_length): print ('can form triangles') # No need to add\ nautomatic line wrapping p = sum ([a_length] B_length C_length]) / / 2 # Note: the parameter to sum is a listarea = m.sqrt (p * (p-a_length) * (p-b_length) * (p-c_length)) print ('the area of the triangle is% fused% (area)) if ((a_length = = b_length) & (b_length = = c_length)): print (' the triangle you entered is equilateral Triangle') elif ((a_length = = b_length) | (b_length = = c_length) |\ (c_length = = a_length)): print ('the triangle you entered is an isosceles triangle') elif ((a_length * a_length + b_length * b_length = = c_length * c_length)\ | (c_length * c_length + b_length * b_length = a_length *) A_length)\ | (a_length * a_length + c_length * c_length = = b_length * b_length): print ('the triangle you entered is a right triangle') else: print ('the triangle you entered is an ordinary triangle') else: print ('cannot form a triangle') input ()

The results show that:

This is the end of the content of "how to use Python to judge the type of triangles". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report