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

beginner Published 7 Jun 2026
Action Steps
  1. Run the script and test it with different input combinations to identify the issues with the current implementation
  2. Modify the script to handle cases where the user enters invalid or missing values for the hypotenuse, opposite, or adjacent sides
  3. Apply the law of sines or cosines to calculate the angle when two sides are known
  4. Test the updated script with various input scenarios to ensure it provides accurate results and handles errors properly
  5. 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
Read full article → ← Back to Reads

Related Videos

How to Train AI to Play Games ? How AI Learns to Play ? Several Methods EXPLAINED
How to Train AI to Play Games ? How AI Learns to Play ? Several Methods EXPLAINED
MaxonShire
Introduction to Machine Learning: Lesson 05
Introduction to Machine Learning: Lesson 05
Stephen Blum
Pytorch Embedding Model Part 1
Pytorch Embedding Model Part 1
Stephen Blum
Introduction to Machine Learning: Lesson 04
Introduction to Machine Learning: Lesson 04
Stephen Blum
Introduction to Machine Learning: Lesson 03
Introduction to Machine Learning: Lesson 03
Stephen Blum
Introduction to Machine Learning: Lesson 02
Introduction to Machine Learning: Lesson 02
Stephen Blum