This is the question:
A triangle can be classified based on the lengths of its sides as equilateral, isosceles or scalene. All 3 sides of an equilateral triangle have the same length. An isosceles triangle has two sides that are the same length, and a third side that is a different length. If all of the sides have different lengths then the triangle is scalene. Write a program that reads the lengths of 3 sides of a triangle from the user. Display a message indicating the type of the triangle
And here is my code, which doesn't work:
#this doesn't work for some reason: s1, s2, s3 = float(input('Enter three sides (separated by comma): ').split(','))
if s1 == s2 and s2 == s3:
print('Equilateral')
elif (s1 == s2 and s2 != s3) or (s1 == s3 and s2 != s3) or (s2 == s3 and s1 != s2):
print('Isosceles')
else:
print('Scalene')
What am I missing? Thanks in advance.
That's because split()
is a string function. You can't use it to an integer or float. So, it needs to be like this
s1, s2, s3 = input('Enter three sides (separated by comma): ').split(',')
This looks like you are trying to cast a list of strings to a list of floats. This does not work.
I would use something like this:
str1, str2, str3 = input('Enter three sides (separated by comma): ').split(',')
s1 = float(str1)
s2 = float(str2)
s3 = float(str3)
Or a list-comprehension:
s1, s2, s3 = [float(s) for s in input('Enter three sides (separated by comma): ').split(',')]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.