如何在此函数中实现公式?

时间:2019-07-14 11:17:55

标签: python function

我已经计算出,为了找到所需的塑料量,我需要计算砖的体积:

高x长x宽

计算后,我需要将此体积乘以每块砖的重量(在这种情况下为7)来获得所需的塑料量:

体积x每立方厘米的重量。

我知道我需要做什么,我只是不知道如何实现它。

def amount_of_plastic(width_brick, height_brick, length_brick, number_of_bricks):
    """Given the dimensions of a brick (width, height, length in cm) and the number of bricks ordered, calculate how much plastic, in grams, is required (if a cubic centimetre weighs 7 grams)."""

    # INSERT YOUR CODE BELOW THIS LINE FOR CALCULATING THE 
    # AMOUNT OF PLASTIC AND RETURNING THE RESULT (DO NOT CHANGE
    # THE HEADER OF THE FUNCTION WHICH HAS BEEN PROVIDED FOR YOU
    # ABOVE)


# DO NOT CHANGE THE CODE BELOW THIS LINE
# The code below automatically tests your function
# following the approach described in 
# Block 2 Part 4 (Page 207 and further).
# Before making any changes to this file, 
# when you run it, you will get an AssertionError. 
# Once you have completed the file with correct
# code, the AssertionError should no longer appear and
# "tests passed" will appear in the shell.


def test_amount_of_plastic():
    """Test the amount_of_plastic() function."""
    # Test for brick with dimensions 0, 0, 0 and 
    # order of 20 bricks
    assert amount_of_plastic(0, 0, 0, 20) == 0

    # Test for brick with dimensions 1, 1, 1 and 
    # order of 0 bricks
    assert amount_of_plastic(1, 1, 1, 0) == 0

    # Test for brick with dimensions 1, 1, 1 and 
    # order of 20 bricks
    assert amount_of_plastic(1, 1, 1, 20) == 140

    # Test for brick with dimensions 1, 2, 3 and 
    # order of 100 bricks
    assert amount_of_plastic(1, 2, 3, 100) == 4200

    print ("tests passed") 


test_amount_of_plastic()

2 个答案:

答案 0 :(得分:1)

return width_brick*height_brick*length_brick*7*number_of_bricks

答案 1 :(得分:0)

def amount_of_plastic(width_brick, height_brick, length_brick, number_of_bricks):

使用输入的宽度,高度,长度,砖数和每1 cm3需要的7 gr的输入,您可以计算所需的总克数:

plasticNeeded = width_brick * height_brick * length_brick * number_of_bricks * 7

然后输出结果

return plasticNeeded