We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

DRY Code

One of the most famous rules for maintainable code is "Don't Repeat Yourself" (DRY): avoid writing the same logic twice! Duplicate code causes problems:

  • If you need to change it, you have to change it in multiple places
  • If you forget to change it in one place, you'll have a bug
  • It's more work to write it over and over again

Assignment

The fight_soldiers function currently repeats its DPS (damage-per-second) calculation. These two lines are almost identical, but the second one has a bug:

soldier_one_dps = soldier_one["damage"] * soldier_one["attacks_per_second"]
soldier_two_dps = soldier_two["damage"] - soldier_two["attacks_per_second"]

Replace both calculations with calls to get_soldier_dps, providing the correct soldier as an argument.

Tip

You can call the function like this:

soldier_one_dps = get_soldier_dps(soldier_one)

Then do the same thing, but for soldier_two.