Python Forum
def functions creation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
def functions creation
#14
I think you think too complicated.

Given is max_volume, max_weight and density.

Then you calculate the volume: volume = max_weight / density
Then you return the minimum of max_volume and volume.

The test with your provided data:
Output:
>>> calculate_possible_volume(1800) 14.555 >>> calculate_possible_volume(380) 67.5
The if, elif, else and for-loop is misleading.
Solving the problem with simple math is simpler.

By the way, your test code is wrong.

def test_volume_per_container():
    """Test the volume_per_container function."""
 
    # Test for gravel at 1800 kg per cubic metre 
    assert volume_per_container(1800) == 14.555
     
    # Test for wood chips at 380 kg per cubic metre
    assert volume_per_container(380) == 67.5
     
    print ("tests passed") 
Should be:

from math import isclose


def test_volume_per_container():
    """Test the volume_per_container function."""
 
    # Test for gravel at 1800 kg per cubic metre 
    assert isclose(volume_per_container(1800), 14.555)
     
    # Test for wood chips at 380 kg per cubic metre
    assert isclose(volume_per_container(380), 67.5)
     
    print ("tests passed") 
Both testes, if you have the right function name, passing.
But the cause why you should always avoid float == float is 0.30000000000000004.com. Tell this your tutor. He should know this.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
def functions creation - by samh625 - Jun-19-2020, 10:59 AM
RE: def functions creation - by pyzyx3qwerty - Jun-19-2020, 02:32 PM
RE: def functions creation - by ndc85430 - Jun-19-2020, 03:09 PM
RE: def functions creation - by samh625 - Jun-19-2020, 10:09 PM
RE: def functions creation - by samh625 - Jun-19-2020, 03:10 PM
RE: def functions creation - by ndc85430 - Jun-20-2020, 03:10 AM
RE: def functions creation - by samh625 - Jun-20-2020, 03:28 PM
RE: def functions creation - by Yoriz - Jun-20-2020, 03:40 PM
RE: def functions creation - by ndc85430 - Jun-21-2020, 08:29 AM
RE: def functions creation - by samh625 - Jun-21-2020, 08:35 AM
RE: def functions creation - by ndc85430 - Jun-21-2020, 08:43 AM
RE: def functions creation - by samh625 - Jun-26-2020, 09:56 AM
RE: def functions creation - by pyzyx3qwerty - Jun-26-2020, 02:46 PM
RE: def functions creation - by DeaD_EyE - Jun-26-2020, 04:24 PM
RE: def functions creation - by samh625 - Jun-26-2020, 10:29 PM
RE: def functions creation - by TGH - Jun-30-2020, 08:28 PM
RE: def functions creation - by niusia - Jul-17-2020, 04:57 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020