In: Computer Science
when the user entered a negative number, you output a message that it was not possible to take the square root of a negative number. This time you will write a Python program that asks the user to enter a non-negative number (float). If the user does enter a negative number, you will use a while loop to continue prompting the user until you get a valid number (i.e. a number >= 0). Once the user enters a valid number, you calculate and print the square root. Test your program twice, once using a negative number and once using a non-negative number.
import math
while(True):
#reading num
num=float(input("enter a non-negative number :"))
#break loop if it is valid
if num>0:
break
#show error messag for negative numbers
print("It is not possible to take the square root of a negative number")
print("Square root of ",num," = ",math.sqrt(num))
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me