Which one of these would be better to calculate overtime pay?

Viewed 181

I want somebody to explain what is the difference between these two was of solving this problem and which one would be better.

Rewrite your pay program using try and except so that your program handles non-numerical input gracefully by printing a message and exiting the program. The following shows two executions of the program:

Enter Hours: 20
Enter Rate : nine
Error, please enter numeric input

Enter Hours: forty
Error, please enter numeric input
input_hours = input('Enter Hours: ')
try:
    hours = float(input_hours)              
except ValueError:
    print('Error, please enter numeric input')
    quit()
input_rate = input('Enter Rate: ')
try:
    rate = float(input_rate)                
except ValueError:
    print('Error, please enter numeric input')
    quit()
if hours < 40:
    pay = rate * hours                      
else:
    overtime = hours - 40                   
    pay = (rate * 40.0) + (1.5 * rate * overtime)
print(pay)

or

try:
    hrs = input('Enter Hours: ')
    hr = float(hrs)
    rate = input('Enter Rate: ')
    rt = float(rate)
    if float(hr) <= 40:
        print(hr * rt)
    else:
        hrr = hr - 40
        rr = hrr * 1.5 * rt
        print(40 * rt + rr)
except:
    print('Error, please enter numeric input')
3 Answers

Let's call them way1 and way2 respectively.

  1. In way1, you are checking the value error after each input, but in way2, you are checking any error(in this case it is most likely to be a value error) after your code snippet.

  2. Way1 checks error after every input, and way2 checks the error as a whole. In python, if it gets any error, the compiler will stop and throws an error.

Suppose, due to human error, you'll get an attribute error, way2 will print:

"Error, please enter numeric input"

But, way1 will give you an error and the code stops working, you'll have to re-run it.

  1. Now let's talk about space and time complexity, both the code have the same complexity

  2. IF someone else wants to understand your code, in a situation when you're not contactable, he/she will easily understand the way2 snippet because it is cleaner and easy to read.

The main difference between these approaches is whether you have a try-catch block around all of the code, or just one around the error-checking. Firstly, the first one splits the error-checking into multiple try-except blocks, whereas the second one puts the whole code block into a single try-except block. Secondly, the first one skips the calculation part of the code with quit() functions, whereas the second one skips it by having it inside of the try block.

I would say that the second approach is cleaner and easier to read. Firstly, you have only one try-except block, which reduces the amount of redundant code that you have. Secondly, it is easier to see what's happening when you skip the code by having it all in the try-except block rather than having the program quit. Finally, the second piece of code is just shorter and less messy and feels more like standard practice to me.

There is repeated code in the first Try Statement. There are two Try and Excepts handling each user input for errors, whereas in the second block of code there is only one. Don't Repeat Yourself (DRY) principle states that duplication in logic should be eliminated via abstraction. Adding additional, unnecessary code to a codebase increases the amount of work required to extend and maintain the software in the future. The second Try statement looks cleaner, easier to read, and is the preferred logical method.

Related