I wrote a simple script to calculate angles in python, but I am having trouble making it properly answer when you don't enter the proper values
📰 Reddit r/learnprogramming
Learn to handle invalid inputs in a Python script for calculating angles in a triangle using trigonometry
Action Steps
- Run the script and test it with different input combinations to identify the issues with the current implementation
- Modify the script to handle cases where the user enters invalid or missing values for the hypotenuse, opposite, or adjacent sides
- Apply the law of sines or cosines to calculate the angle when two sides are known
- Test the updated script with various input scenarios to ensure it provides accurate results and handles errors properly
- Configure the script to provide informative error messages to the user when invalid inputs are detected
Who Needs to Know This
A junior developer or a student learning Python can benefit from this lesson to improve their skills in handling user inputs and implementing mathematical formulas in code. This is relevant for anyone working on projects that involve user interactions and calculations.
Key Insight
💡 To create a robust script, it's essential to anticipate and handle potential errors and invalid inputs, providing a better user experience
Share This
Improve your Python skills by learning to handle invalid inputs in a script for calculating angles in a triangle #Python #Trigonometry
Key Takeaways
Learn to handle invalid inputs in a Python script for calculating angles in a triangle using trigonometry
Full Article
import math print("Fill in the values (leave - if it is unknown):") hypotenuse = input("Hypotenuse:") opposite = input("Opposite:") adjacent = input("Adjacent:") if adjacent == "-": if opposite == "-" or hypotenuse == "-": print("Enter atleast 2 numbers.") else: hypotenuse = float(hypotenuse) if hypotenuse > opposite: angle = math.degrees(math.asin(opposite/hypotenuse)) print("Angle is: " + str(angle)) else: print("Impossible triangle. Check the numbers
DeepCamp AI