210k views
1 vote
Answer coding question please

Answer coding question please-example-1

1 Answer

3 votes

Answer:

No, see below for explanation.

Step-by-step explanation:

(Attachments are added for visual reference.)

The code will have NameError if you decide to run as seen in the first attachment. If you notice, it will say that the variable cost is not defined.

This happens because we only define the variable cost inside a function totalCost(a, b). That means the variable will only work inside a function itself and if it's outside of function, the variable will not be able to be called outside of its range.

There are several ways to fix. See the second and third attachment as you notice that both codes work and will have same output. The difference is that the first code will rely on function call and the second will rely on variable cost

In simple answer, the code given will not work because the variable is only defined in a function itself and not globally outside of function..

Answer coding question please-example-1
Answer coding question please-example-2
User Ravi Makvana
by
4.3k points