Home > Mobile >  Struggling to figure out where to insert perentheses
Struggling to figure out where to insert perentheses

Time:12-30

Hey guys I need to figure out where the parentheses fit into this code for the following expression and am a little confused to where the parentheses actually goes into. I keep getting it wrong.

Expression

x = float(input('Enter a value for x: '))

# Insert parentheses in the following line to fix the expression.
y = x - 1 ** 0.5   1 / 5

print('y = '   str(y))

CodePudding user response:

y = ( ( (x - 1 ) ** 0.5 )   1 ) / 5

CodePudding user response:

Because of the order of evaluation in python, you actually need three sets of them.

Like this :

y = (((x - 1) ** 0.5)   1 )/ 5

CodePudding user response:

Parentheses around the square root and the numerator should be clear.

y = ((x-1)**0.5   1) / 5

Additionally, you could import 'math'. This lets you use sqrt() as follows:

y = (math.sqrt(x-1)   1) / 5
  • Related