Форум сайта python.su
Из коммандной строки запускаем скрипт с 3 параметрами. Они соотвествуют длинам треугольника.
Определить можно ли построить треугольник с такими длинами. Если да, то какой треугольник получим. (обычный, равнобедренный, равносторонний)
Я реализовал это сл образом. Жду комментариев по улучшению или совета о том, как улучшить данный код.
import sys
input_values = sys.argv[:]
def what_type_triangle(a,b,c):
if (a == b) and (b != c) or (b==c) and (a!=b): #ravnobedrenniy treugol
print 'Triangle with sides: %s %s %s is isosceles' % (a,b,c)
elif (a==b==c):
print 'Triangle with sides: %s %s %s is equilateral' % (a,b,c)
else:
print 'Triangle with sides: %s %s %s is common' % (a,b,c)
def is_it_triangle(length_sides):
length_sides.sort()
a = length_sides[0]
b = length_sides[1]
c = length_sides[2]
if (a<b+c) and (b<a+c) and (c<a+b) and (a>0) and (b>0) and (c>0):
print ('Yeah, you have got triangle')
what_type_triangle(a,b,c)
else:
print ('No!')
def make_digits(values):
nums = []
for value in values[1:]:
try:
ret = float(value)
nums.append(ret)
except ValueError:
sys.exit('bad data for this side: %s' % value )
print (nums)
is_it_triangle(nums)
if __name__ == '__main__':
make_digits(input_values)
Офлайн
esalЛучше такdef what_type_triangle(a,b,c):
if (a == b) and (b != c) or (b==c) and (a!=b): #ravnobedrenniy treugol
print 'Triangle with sides: %s %s %s is isosceles' % (a,b,c)
elif (a==b==c):
print 'Triangle with sides: %s %s %s is equilateral' % (a,b,c)
else:
print 'Triangle with sides: %s %s %s is common' % (a,b,c)
def what_type_triangle(a,b,c):
if (a==b==c):
print 'Triangle with sides: %s %s %s is equilateral' % (a,b,c)
elif (a == b) or (b == c) or (a==c): #ravnobedrenniy treugol
print 'Triangle with sides: %s %s %s is isosceles' % (a,b,c)
else:
print 'Triangle with sides: %s %s %s is common' % (a,b,c)
Офлайн
import sys
a,b,c = sorted(map(float,sys.argv[1:]))
if a<=0 or a+b<=c:
msg='плохой'
elif a==c:
msg="равносторонний"
elif a==b or b==c:
msg="рвнобедренный"
else:
msg="общий"
print msg
Отредактировано (Апрель 18, 2011 20:07:03)
Офлайн