Back to Freecodecamp

Challenge 162: Energy Consumption

curriculum/challenges/english/blocks/daily-coding-challenges-python/694596b0585c11170ac7c7fa.md

latest2.0 KB
Original Source

--description--

Given the number of Calories burned during a workout, and the number of watt-hours used by your electronic devices during that workout, determine which one used more energy.

To compare them, convert both values to joules using the following conversions:

  • 1 Calorie equals 4184 joules.
  • 1 watt-hour equals 3600 joules.

Return:

  • "Workout" if the workout used more energy.
  • "Devices" if the device used more energy.
  • "Equal" if both used the same amount of energy.

--hints--

compare_energy(250, 50) should return "Workout".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(compare_energy(250, 50), "Workout")`)
}})

compare_energy(100, 200) should return "Devices".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(compare_energy(100, 200), "Devices")`)
}})

compare_energy(450, 523) should return "Equal".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(compare_energy(450, 523), "Equal")`)
}})

compare_energy(300, 75) should return "Workout".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(compare_energy(300, 75), "Workout")`)
}})

compare_energy(200, 250) should return "Devices".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(compare_energy(200, 250), "Devices")`)
}})

compare_energy(900, 1046) should return "Equal".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(compare_energy(900, 1046), "Equal")`)
}})

--seed--

--seed-contents--

py
def compare_energy(calories_burned, watt_hours_used):

    return calories_burned

--solutions--

py
def compare_energy(calories_burned, watt_hours_used):
    workout_energy = calories_burned * 4184
    device_energy = watt_hours_used * 3600

    if workout_energy > device_energy:
        return "Workout"
    if device_energy > workout_energy:
        return "Devices"
    return "Equal"