Learn Before
Code
Examples of Hard Coding
We see that the result is correct given the following hard code:
x = 4 y = 10 avg = 7 print(avg) #Prints 7
But If we change the values assigned to x and y assuming avg is still set to 7:
x = 5 y = 120 print(avg) #Still Prints 7
The result would not apply to all situations since the logic is flawed; avg is simply set to 7, not averaged to 7. Therefore, the appropriate solution would be:
x = 4 y = 10 avg = (x + y) / 2 print(avg) #Prints 7
If we changed the values of x and y, our result would still be correct with non-hard code:
x = 12 y = 20 avg = (x + y) / 2 print(avg) #Prints 16
0
1
Updated 2020-09-03
Tags
Data Science