Design a program to calculate the salaries of a company's employees based on the following data:

  • Number of hours worked.
  • The work shift performed: Morning (m), Afternoon (t), Night (n).
  • The ordinary hourly rate for each of the workers ($ 37.0).

For the calculation of the gross salary, take into account that the afternoon shift is paid at $1.20 more than the ordinary rate, the evening shift is paid at $1.20 more than the ordinary rate, and the night shift is paid at $1.50 more than the ordinary rate.

For the calculation of the net salary, certain discounts are made only to those on the night shift according to the following table:

gross salary discount
From 2000 to 5000 15%
From 8000 to 10000 17%

You want to print the net salary of each worker.

ht = int(input('Number of hours worked: ')) 
tt = str(input('\nm. Morning\nt. Afternoon\nn. Night\n\nOption:: '))

if tt == 'm':
    r = (0)
elif tt == 'a':
    r = float(1.2*ht)
elif tt == 'n':
    r = float(1.5*ht)

s1 = float(ht*37)
sb = float(s1+r)

if r == 0:
    sn = float(sb)
elif r == (1.2*ht):
    sn = float(sb)
elif r == (1.5*ht):
    if sb >= 2000 and sb <= 5000:
        sn = float(sb*0.15)
    elif sb >= 8000 and sb <=10000:
        sn = float(sb*0.17)
        sn = float(sb)

print('Net salary:',sn)
  • \$\begingroup\$ Welcome to Code Review! I changed the title so that it describes what the code does per site goals: "State what your code does in your title, not your main concerns about it.". Please check that I haven't misrepresented your code, and correct it if I have. \$\endgroup\$ Jun 4, 2021 at 7:01
  • \$\begingroup\$ Is there an "evening" shift (mentioned in the hourly rates, but not in the list of shifts)? \$\endgroup\$ Jun 4, 2021 at 7:02
  • \$\begingroup\$ This should likely be tagged "homework" \$\endgroup\$
    – Reinderien
    Jun 4, 2021 at 18:25

2 Answers 2


Why do we convert hours worked to int? It seems unethical to steal the workers' partial hours.

If we don't enter a valid shift identifier, then we silently pay the lowest rate instead of reporting the error.

The variable names are completely uninformative. It's hard to work out what each means.

Comparing floating-point numbers for equality (e.g. r == (1.2*ht)) is fragile and shouldn't be done. In this case, we seem to be using it as a proxy for testing tt, so just replace (e.g. tt == 'a'). Given that the first two cases are the same, we only need to test for tt == 'n'.

All the numeric quantities in the question are baked into the code (we're mixing mechanism with policy). Work out how we can separate these data, so that it's easier to change the rates of pay and discounts in future when these values need updating.


My guess is that this is homework, though you haven't specified. There's at least one major problem with the question you were given: my read is that there are actually four shifts, "evening" included. Also "salary" seems like a strange term for hourly pay. Salaried employees usually don't track hours, though this may be regional.

It's highly doubtful that a "salary discount of 15%" means "multiply the salary by 15%"; I would expect instead "multiply the salary by (1 - 15%)".

Consider adding some structure to your program by representing the shift types as an enumeration. Also, you should be calling into the locale-specific currency formatter.

All of these, with most of @TobySpeight's feedback also included, suggest:

from enum import Enum
from locale import currency, setlocale, LC_ALL
from typing import Dict

setlocale(LC_ALL, '')

RATES: Dict[str, float]

class Shift(Enum):
    MORNING = 'm'
    AFTERNOON = 'a'
    EVENING = 'e'
    NIGHT = 'n'

    global RATES
    RATES = {
        MORNING: 0,
        AFTERNOON: 1.20,
        EVENING: 1.20,
        NIGHT: 1.50,

    def rate(self) -> float:
        return BASE_RATE + RATES[self.value]

    def net_pay(self, hours: float) -> float:
        gross = hours * self.rate
        if self is not self.NIGHT:
            return gross

        if 2000 <= gross <= 5000:
            discount = 0.15
        elif 8000 <= gross <= 10000:
            discount = 0.17
            discount = 0
        return gross * (1 - discount)

    def from_stdin(cls) -> 'Shift':
        prompt = '\n'.join(
            f'{s.value}. {s.name.title()}'
            for s in cls
        ) + '\nOption: '
        return cls(input(prompt))

hours = float(input('Number of hours worked: '))
shift = Shift.from_stdin()
print('Net pay:', currency(shift.net_pay(hours)))

which produces

Number of hours worked: 100
m. Morning
a. Afternoon
e. Evening
n. Night
Option: n
Net pay: $3272.50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.