python fundamentals 2.4 code practice question 1

write the code to input a number and print the square root. use the absolute value function to make sure that if the user enters a negative number, the program does not crash.

sample run: -16
sample output : 4.0​

Respuesta :

Answer:

import math

inputNumber = float(input('Please input a number'))

inputNumber = abs(inputNumber)

print(math.sqrt(inputNumber))

Explanation:

hey GSPAULING! lets do some PYTHON and PYTHON is epic because its logo has those python looking things. theres one thats blue and one thats yellow you know what im talking about right? ANYWAYS LETS WRITE SOME CODE

First how do we start?

We need an input statement right?

inputNumber = int(input('Please input a number'))

ok so the line above which starts with "input" is the first line of our code, it's gonna ask the user for a number and the number will become the variable called "inputNumber" and it will be an integer type of variable, right?

and now the absolute value function is: abs()

so lets incorporate that in the next line

inputNumber = abs(inputNumber)

so now the negative numbers are going to be turned into positive numbers.

ok but now lets not forget to import the math module, you should actually put this line at the very beginning:

import math

ok now we can find the square root of the inputNumber variable and print it:

print(math.sqrt(inputNumber))

so the final program will be as follows:

import math

inputNumber = float(input('Please input a number'))

inputNumber = abs(inputNumber)

print(math.sqrt(inputNumber))

ACCESS MORE