Q. In each of the following parts there is an error in the Python code. Identify the error by name and describe the problem. (Each piece of code is prefixed with a brief description of the programmer's intention.)


(i) The following code attempts to compute the product of m and x added to b and assign that value to y.
y = mx + b
(ii) The following code attempts to compute the first-order effects of some physical process. You may assume that equation is correct.


cofactor = alpha * x * x
1storder = 1.0 / cofactor
2ndorder = 2.0 ** 1storder

Answer :-

(i)
Correct code :-

y = m * x + b

Explanation :- If you want to multiply two variable then you have to use ‘*’ operator. 

(ii)
Correct code :-

cofactor = alpha * x * x
first_st_order = 1.0 / cofactor
second_st_order = 2.0 ** first_st_order

Explanation :- Python will give error if variable name is started with number.

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post