fork download
  1. import math
  2.  
  3. def calculate_square_root():
  4. user_input = input("Enter a number to calculate its square root: ")
  5.  
  6. try:
  7. # Try to convert the input to a float
  8. number = float(user_input)
  9.  
  10. if number < 0:
  11. # Handle negative numbers
  12. print("Error: Cannot calculate the square root of a negative number.")
  13. else:
  14. # Calculate and display the square root
  15. square_root = math.sqrt(number)
  16. print(f"The square root of {number} is {square_root}.")
  17.  
  18. except ValueError:
  19. # Handle non-numeric input
  20. print("Error: Please enter a valid numeric value.")
  21.  
  22. # Run the function
  23. calculate_square_root()
Success #stdin #stdout 0.03s 9692KB
stdin
7
stdout
Enter a number to calculate its square root: The square root of 7.0 is 2.6457513110645907.