Python Forum

Full Version: What am I doing wrong? New to Python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everyone I have recently been introduced to Python and I am attempting some activities for beginners but I have become stuck on one part.
Basically my task is to create a function that takes a list of four input values and from that an output value is formed. Each number in the list should be multiplied by the next number in the list e.g. [5,4,3,2] would be 5 * 4 * 3 * 2 then the total of this sum is then multiplied by 7 and that is the final answer. (This program should work for any combination in the list) Below I have shown my attempt. I could be so far off but it'll be good to learn from;

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)

    total_weight = 0

    amount_of_plastic = [5, 2, 2, 20]

    for item in amount_of_plastic:
        total_weight = item * item
        total_weight = item * 7
        
    return(total_weight)
Any help is greatly appreciated.
This should work. The count variable is used to check if the program is on the first item. If so, it sets total_weight equal to that item. Then count is increased by one so the program now multiplies the rest. After, it is multiplied by seven for the end result.
amount_of_plastic = [5, 2, 2, 20]
total_weight = 0
count = 0
for item in amount_of_plastic:
    if count != 0:
        total_weight *= item
    else:
        total_weight = item
        count = 1
total_weight *= 7
Thank you for your help, still a couple of issues with it, when attaching a return(total_amount) at the end of the code I get an error message "'return' outside function". Also I tested by printing the result, by doing this I noticed that it shows 4 different calculations one below the other representing each item in the list multiplied by 7. My intention is to have one number showing and that being the overall total.
When I print total weight, it says 2016. Also return total_weight is the correct way to return.
The sum is now working correctly but '
return total_weight
' is still presenting the error 'return' outside function
Hold your horses gentleman.

Please read carefully:

"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)"

Then observe parameters given in function (width_brick, height_brick, length_brick, number_of_bricks) and comment about it:
# DO NOT CHANGE
# THE HEADER OF THE FUNCTION WHICH HAS BEEN PROVIDED FOR YOU
# ABOVE



Therefore this function is as simple as:

def amount_of_plastic(width_brick, height_brick, length_brick, number_of_bricks):
    return width_brick * height_brick * length_brick * number_of_bricks * 7 
EDIT: this is useful read: What is the difference between arguments and parameters?
That solved it. Looks like I was overthinking it. Thanks very much for the help guys.