Question

In: Computer Science

when the user entered a negative number, you output a message that it was not possible...

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.

Solutions

Expert Solution

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


Related Solutions

Write a C++ program that finds the minimum number entered by the user .The user is...
Write a C++ program that finds the minimum number entered by the user .The user is allowed to enter negative numbers 0, or positive numbers. The program should be controlled with a loop statement. Break statements is not allowed to break from loop. After each number inputted by the user, The program will prompt user if they need to enter another number. User should enter either Y for yes or N for no. Make Sure the user enters either Y...
Display a simple message "Welcome" on your demo webpage. When the user hovers over the message,...
Display a simple message "Welcome" on your demo webpage. When the user hovers over the message, a popup should be displayed with a message "Welcome to our new WebPage!!!".
The program prompts a user for the number of contestants entered in this year’s and last...
The program prompts a user for the number of contestants entered in this year’s and last year’s Greenville Idol competition, and then it displays the revenue expected for this year’s competition if each contestant pays a $25 entrance fee. The programs also display a statement that compares the number of contestants each year. Now, replace that statement with one of the following messages: If the competition has more than twice as many contestants as last year, display The competition is...
Write a program that allows the user to search the array to find the number entered
Write a program that allows the user to search the array to find the number entered
write a program that: 1) asks the user to enter grades, a negative number is the...
write a program that: 1) asks the user to enter grades, a negative number is the signal to stop entering. (while loop) - add each grade to a list (do not add the negative number to the list) after the while loop to enter grades is finished: 2) use a for loop on the list to calculate the average of all numbers in the list 3) use a for loop on the list to find the largest number in the...
2. Create a C++ program that converts the number of American dollars entered by the user...
2. Create a C++ program that converts the number of American dollars entered by the user into one of the following foreign currencies: Euro, British pound, German mark, or Swiss franc. Allow the user to select the foreign currency from a menu. Store the exchange rates in a four-element double array named rates. Notice that the menu choice is always one number more than the subscript of its corresponding rate. For example, menu choice 1's rate is stored in the...
If a negative externality exists, then there is a __________ when society produces the market output...
If a negative externality exists, then there is a __________ when society produces the market output instead of the socially optimal output. This exists because the __________ to sellers and third parties are __________ the __________ derived by buyers. a. net social benefit; costs; greater than; benefits b. net social cost; benefits; less than; costs c. net social cost; costs; greater than; benefits d. net social cost; costs; less than; benefits e. none of the above
Create a C++ program that will prompt the user to input an integer number and output...
Create a C++ program that will prompt the user to input an integer number and output the corresponding number to its numerical words. (From 0-1000000 only) **Please only use #include <iostream>, switch and if-else statements only and do not use string storing for the conversion in words. Thank you.** **Our class is still discussing on the basics of programming. Please focus only on the basics. Thank you.** Example outputs: Enter a number: 68954 Sixty Eight Thousand Nine Hundred Fifty Four...
Create a C++ program that will prompt the user to input an integer number and output...
Create a C++ program that will prompt the user to input an integer number and output the corresponding number to its numerical words. (From 0-1000000 only) **Please only use #include <iostream> and switch and if-else statements only. Thank you. Ex. Enter a number: 68954 Sixty Eight Thousand Nine Hundred Fifty Four Enter a number: 100000 One Hundred Thousand Enter a number: -2 Number should be from 0-1000000 only
When the price and output decisions of one firm include the possible price and output reactions...
When the price and output decisions of one firm include the possible price and output reactions of the firm's rivals, the market is    a. monopolistically competitive characterized by non-price competition.      b. perfectly competitive characterized by collusion.      c. a monopoly characterized by differentiated products.      d. an oligopoly characterized by mutual interdependence.   The theory of monopolistic competition predicts that in long-run equilibrium a monopolistically competitive firm will:    a. operate at minimum long-run average cost.      b. produce...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT