-2

I have an exercise where they give us 2 categories (A,B), each category has an increase in salary. Category A increases 1%, Category B increases 1.5%. I was thinking in this:

salary = float(input("whats your salary? "))
Category = input("whats your category(A,B)? ")
final_salary = salary + (salary* #the category chosen)

How can i redirect the category chosen so it can multiply for the salary??

  • 1
    Does this answer your question? [How to emulate a switch-case in python using dictionaries](https://stackoverflow.com/questions/48190949/how-to-emulate-a-switch-case-in-python-using-dictionaries) – saxbophone Aug 04 '22 at 01:16

5 Answers5

1

you can use a dictionary. using lower you can avoid errors due to mismatches between the input and the dictionary keys

increase = {'a': 0.01, 'b': 0.015}

salary = float(input("whats your salary? "))
category = input("whats your category(A,B)? ")

final_salary = salary*(1+increase[category.lower()])
mauro
  • 504
  • 3
  • 14
  • 1
    I'd recommend using `.get` to take into account for keys not present(for example category 'c') `final_salary = salary*(1+increase.get(category.lower(), 0))` – mad_ Aug 01 '22 at 14:59
  • that's a good idea. another solution could be to raise an error in case the category is not existing. that really depends on how OP will use the code – mauro Aug 01 '22 at 15:02
0

Use a dictionary:

category_rates = {"A": 0.01, "B": 0.015}
final_salary = salary + (salary * category_rates[Category])
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Sayse
  • 42,633
  • 14
  • 77
  • 146
0

Create another variable called increase, and assign it a proper value depending on the category input.

Category = input("whats your category(A,B)? ")
if Category == "A":
    increase = 0.01
elif Category == "B":
    increase = 0.015
final_salary = salary + (salary* increase)
John Gordon
  • 29,573
  • 7
  • 33
  • 58
0
salary = float(input("whats your salary? "))
Category = input("whats your category(A,B)? ")

if Category == "A":
    increase = 0.01
elif Category == "B":
    increase = 0.015
else:
    print("incorrect Category")

final_salary = salary + (salary * increase)
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Aleksei
  • 77
  • 8
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 02 '22 at 22:06
0
category_dictionary = {'A':1,'B':1.5}
salary = float(input("whats your salary? "))
Category = input("whats your category(A,B)? ")
if Category in category_dictionary :
    new_salary = salary + salary * (category_dictionary[Category]/100) 
    print('New salary : ' + str(new_salary))
else :
    print('Wrong input')
jmr28
  • 1
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 03 '22 at 02:44