Home > Software engineering >  How can i fix this part
How can i fix this part

Time:12-28

Using Python, write a program that will check the employees years in service and office. The user will input number for years and in service and the following offices (it, acct, hr) Check the following conditions. Office IT ACCT HR Years More than or equal 10 years 10000 12000 15000 Below 10 years 5000 6000 7500

i already have my code but im having a hard time to.. make it more specific

# take the input from user
year = int(input('Please enter the years: '))
office = input('Please enter the office name: ')
# if-else condition
if (office.upper() == "IT" or office.lower() == "it") or (office.upper() == "ACCT" or office.lower() == "acct") or (office.upper() == "HR" or office.lower() == "hr"):
    {
        print("Employee office is: "  office)
    }
else:
    {
        print("The wrong office name entered")
    }
# if-else condition
if year >=10:
    {
        print("10000 12000 15000")
    }
else:
    {
        print("5000 6000 7500")
    }

but the expected result should be like this:

enter years in service: 15
enter office : IT
salary based on year/exp: 10000

CodePudding user response:

All you need is to repeat the year if-statements in the individual office name if-statements:

year = int(input('Please enter the years: '))
office = input('Please enter the office name: ')

print("\nsalary based on year/exp: ",end='')
if office.upper() == "IT" or office.lower() == "it":
    if year >=10: print(10000)
    else: print(5000)
elif office.upper() == "ACCT" or office.lower() == "acct":
    if year >=10: print(12000)
    else: print(6000)
elif office.upper() == "HR" or office.lower() == "hr":
    if year >=10: print(15000)
    else: print(7500)
else:
    print("The wrong office name entered")

CodePudding user response:

You can use a dictionary to store key and value pairs, where you can access the value by using the key. So let the key be the office name and value a tuple containing salary for less than 10 years and for more or 10 years. Then ask for the office name and immediately lower the returned value to make things easier. Then ask for years in service and convert the returned value to an integer to be able to make comparisons. Then get the value from the dictionary and if the key doesn't exist (incorrect office name), return a tuple of two values so that later usage of indexes doesn't raise an exception. Then based on the years print either the first value or second value in the tuple:

office_salary = {
    'it': (5000, 10_000),
    'acct': (6000, 12_000),
    'hr': (7500, 15_000)
}

office = input('Enter office name: ').lower()
years = int(input('Enter years in service: '))

salaries = office_salary.get(office, (None, None))
if years < 10:
    print(salaries[0])
elif years >= 10:
    print(salaries[1]

CodePudding user response:

If I understood you correctly:

# take the input from user
year = int(input('Please enter the years: '))
office = input('Please enter the office name: ')
# if-else condition
if (office.upper() == "IT"):
    if (year >= 10):
        print("10000")
    else:
        print("5000")
elif (office.upper() == "HR"):
    if (year >= 10):
        print("12000")
    else:
        print("6000")
elif (office.upper() == "ACCT"):
    if (year >= 10):
        print("15000")
    else:
        print("7500")
else:
    print("The wrong office name entered")

If not, you need to clarify further.

  • Related