fork download
  1. import math
  2.  
  3. while True:
  4. try:
  5. number = float(input("Enter a number: "))
  6. if number < 0:
  7. print("Error: Cannot calculate the square root of a negative number.")
  8. else:
  9. square_root = math.sqrt(number)
  10. print("The square root of", number, "is", square_root)
  11. break # Exit the loop if successful
  12. except ValueError:
  13. print("Error: Invalid input. Please enter a valid number.")
Success #stdin #stdout 0.03s 9632KB
stdin
7
stdout
Enter a number: The square root of 7.0 is 2.6457513110645907