![]() ![]() ![]() # Hint: use a set here rather than many nearly identical comparisons Sides is a list of 3 floats representing the sides of a triangle. """side is a float representing the side of a triangle. """Prompt the user for a float until they provide a valid one. Here's an easy framework to consider that's essentially modular. One of the other answers goes a long way to improving it with a Triangle class, but I suspect that that's a little farther ahead of where you are right now. It's a little hard to approach your code because you have a large amount of repetition. This will save you and your readers from tears. Practice turning repetitious code into a function. Now you can just call this function 3 times: side1 = ask_side_length("first") I excluded negative inputs as well, since I don't think a negative side-length makes sense in most contexts. Previously, your entire program would stop if any input was bad. I'm looping while this single request for input is bad. This isn't code-golf! Shorter code does not necessarily mean better code. I'm using the True constant, since a Boolean comparison to achieve the same seems convoluted. That means you now have a reusable bit of code you can use anywhere you want, without needing to retype it or copy and paste. Then ask_side_length could be defined in terms of that function. You could, for practice, create a function that accepts general input from a user, validates it, and loops when validation fails. ![]() This could be generalized, but for the sake of the review, I'm going to leave it like this. Length = float(input("How long is the " + side_message + " side of the triangle?")) That can be reduced down to a single function that "safely" asks the user for input: def ask_side_length (side_message): When asking for input, you write out a full try/ except for each side, even though they're really all the same. One major improvement you can make that will instantly neaten up your code is changing the way you take input from the user. It gives you more small bits of code that can be reused in other places. I'm of the opinion that the more functions you have, the better (within reason). What if you ever wanted to do the calculations based off of data from a file? Or the internet? Create a function that calculates whether or not a given triangle is isosceles, pass the data to it, and have it return True/ False. Your code isn't modular it's just a long script of calculations and printing. This will be the main concern of my review. The major thing that stood out to me was the sheer amount of repetition in your code. If (float(side1)+float(side3))float(side2) and float(side3)>float(side1): If (float(side2)+float(side3))float(side1) and float(side2)>float(side3): Print("This is not an isosceles triangle!")Įlif float(side1)>float(side2) and float(side1)>float(side3): Side3 = float(input("How long is the third side? ")) Side2 = float(input("How long is the second side? ")) Side1 = float(input("How long is the first side of the triangle? ")) They will be the sides of a triangle, and I will tell you if it is an isosceles triangle or not.") These numbers can be integers or decimals. Print("I am going to ask you for three numbers. It will make sure that none of the sides are 0, it will check whether the lengths of the sides are even possible and it will make sure that the user actually inputted a number. This code will check everything I can think of to make sure that it will actually work and if it is an isosceles triangle. I think I have pretty much the best code you can get when it comes to this particular task, but I'm always open to improvement. ![]()
0 Comments
Leave a Reply. |