Python Forum
def functions creation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
def functions creation
#11
Besides the syntax errors, the calculation seems wrong. Do you even need if, elif and else in the first place? Again, how would you do the calculation on paper? Are you sure the expected values on lines 23 and 26 are correct (particularly 26)?

To stop us guessing, it might actually help if you posted the text of your assignment verbatim.
Reply
#12
h! sorry for the long reply, been stuck in work,

Imagine that a company in the freight transport business has commissioned you to solve the following problem. Given bulk cargo with a certain weight per m3, compute the volume of that bulk cargo in m3 that a single 40-foot standard container can hold.

A standard 40-foot container:

has an internal volume of 67.5 m3
a maximum net load of 26199 kg .
For instance, 1 m3 of gravel weighs 1800 kg. Because the maximum net load of a container is 26199 kg, a single container can only hold 14.555 m3 of gravel, otherwise it would exceed the maximum net load.

For comparison, 1 m3 of wood chips weighs 380 kg. Because the maximum net load of a container is 26199 kg, based on the maximum weight, it is allowed to hold 68.94 m3 of wood chips. However, the internal volume of a single container is only 67.5 m3, so a single container can hold 67.5 m3 of wood chips.

In answer to this question you will need to decompose the problem, write an algorithm, and implement the algorithm as a single python function that returns the total volume that can be held by single container.

i have been recommend by my tutor the use of an if else then.
Reply
#13
An if else looop would be the best option for you
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#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
#15
Awesome stuff! thank you! i shall let them know! i shall work on my python and look at the books everyone has recommended :)
Reply
#16
Hi,
How did you get on with completing this task? Did you manage to get it through the test phase?

It works fine until I run it through the test and cannot figure out why. Any areas to look at would be greatly appreciated.
Reply
#17
The below will work :)


def volume_per_container(kg_cargo_per_cubic_metre):
"""Given the kg of cargo per cubic metre, calculate how many cubic metres of cargo can be stored in a single container."""


max_load = 26199
max_volume = 67.5

volume = max_load/kg_cargo_per_cubic_metre

if volume <= max_volume:
return volume

else:
return max_volume
Reply


Forum Jump:

User Panel Messages

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