Python Forum
def functions creation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
def functions creation
#1
Hey all, so my course given me another tricky one, i think i get the idea of it, just trying to finding the right way of writing it. My tutor has said that a if, else then statement is best to be used.

The take is to find out the kg of cargo to cubic meters and how many can fit in to a container.
So the container is 67.5 volume and has a net weight of 26199kg.

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."""
if 26199 == 67.5:
    1800 * 14.555 = 26199 <= 67.5
elif:
    380 * 68.94 = 26.197 >= 67.5
elif:
    380 * 67.5 = 25.650 <= 67.5

    return 
    
    
    # INSERT YOUR CODE BELOW FOR CALCULATING THE 
    # TOTAL VOLUME AND RETURNING THE RESULT (DO NOT CHANGE
    # THE HEADER OF THE FUNCTION WHICH HAS BEEN PROVIDED FOR YOU
    # ABOVE)
    

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") 
  
test_volume_per_container()
Am i heading in the right direction? Not looking for the answer, mainly just pointers if i am doing this completely wrong. I get syntax errors but i cant figure out a better way of writing this code out.

Many thanks Sam
Reply
#2
why do you pass an empty return statement?
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
#3
A few things:

1. Your indentation on lines 3-10 is incorrect.
2. When will the condition on line 3 ever be True?
3. Lines 4, 5 and 6: you compute some values and throw the results away. Why? Also, those expressions don't really make any sense: at the very least, you're using =, which is for assignment and aren't using any variables so assignment doesn't make sense. Did you mean to compare equality? Even in that case, I'm not really sure what the expressions as a whole are meant to do, given there's also a <= in them.
4. You pass a parameter to the function, but never use it anywhere in the function.
5. Your elifs don't have conditions. Remember that elif means "else if", so needs to have a condition to make sense.
6. Are you using pytest as your test framework? In any case, you shouldn't need the print on line 28; the test framework will report which tests have passed and failed. Technically, you only have one test, but the framework will show you when either of the assertions fail anyway.
Reply
#4
I forgot to put that in, ?‍♀️?‍♀️?‍♀️?‍♀️
Reply
#5
(Jun-19-2020, 03:09 PM)ndc85430 Wrote: A few things:

1. Your indentation on lines 3-10 is incorrect.
2. When will the condition on line 3 ever be True?
3. Lines 4, 5 and 6: you compute some values and throw the results away. Why? Also, those expressions don't really make any sense: at the very least, you're using =, which is for assignment and aren't using any variables so assignment doesn't make sense. Did you mean to compare equality? Even in that case, I'm not really sure what the expressions as a whole are meant to do, given there's also a <= in them.
4. You pass a parameter to the function, but never use it anywhere in the function.
5. Your elifs don't have conditions. Remember that elif means "else if", so needs to have a condition to make sense.
6. Are you using pytest as your test framework? In any case, you shouldn't need the print on line 28; the test framework will report which tests have passed and failed. Technically, you only have one test, but the framework will show you when either of the assertions fail anyway.

hey, very new to python, i sort put it together and trying to guess my through it, i kept getting syntax errors on every line, with line 28: its what the uni have programmed so i wont touch that XD.

most of it is a guess, the course dont really explain things all that well. i will need to keep guessing it, if this post is to much or not aloud please remove XD but if not any tips or hints would be amazing.

Sam
Reply
#6
Surely you've been recommended a book? That should help with the syntax, else there are many resources online.
Reply
#7
we've got books from the course, but they explain things very poorly. i've edited my code so looks a bit better, but i keep getting a syntex error with the elif statement and i cant figure out why.
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."""
if kg_cargo_per_cubic_metre (26199 == 67.5):
    26199/380 == 68.94
    print ('wood does not fit the volume of container')
else:
    25650/380 == 67.5
    print ('wood fits the volume of container')
elif
    26199/1800 == 67.5:
    print ('Gravel fits the volume of container')

return volume_per_container(kg_cargo_per_cubic_meter) 
    
    
    # INSERT YOUR CODE BELOW FOR CALCULATING THE 
    # TOTAL VOLUME AND RETURNING THE RESULT (DO NOT CHANGE
    # THE HEADER OF THE FUNCTION WHICH HAS BEEN PROVIDED FOR YOU
    # ABOVE)
    
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") 
  
test_volume_per_container()
Reply
#8
Python documents on the use of if
Reply
#9
I don't know why you're doing any printing in the function. The comments on lines 16-19 and the string on line 2 suggest you're meant to do a calculation and return the result. Your function doesn't do that at all and the indentation is still wrong. Also, line 13: why are you trying to call your function recursively? That doesn't make any sense either. I also think you're confused about the calculation you need to do. How would you do those with pen and paper, for a given density (kg_cargo_per_cubic_metre) and the other information you're given?

Which books have you been recommended? You might like to try Think Python, which is available for free online.
Reply
#10
I find it so confusing, its the uni's owm course books. I shall try that one yoi recommended. So what is it that i am doung wrong? If you don't mind me asking. I am just winging it atm
Reply


Forum Jump:

User Panel Messages

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