What am I doing wrong? New to Python - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: What am I doing wrong? New to Python (/thread-19493.html) |
What am I doing wrong? New to Python - Stthmc_1995 - Jul-02-2019 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. RE: What am I doing wrong? New to Python - SheeppOSU - Jul-02-2019 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 RE: What am I doing wrong? New to Python - Stthmc_1995 - Jul-02-2019 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. RE: What am I doing wrong? New to Python - SheeppOSU - Jul-02-2019 When I print total weight, it says 2016. Also return total_weight is the correct way to return.
RE: What am I doing wrong? New to Python - Stthmc_1995 - Jul-02-2019 The sum is now working correctly but ' return total_weight' is still presenting the error 'return' outside function RE: What am I doing wrong? New to Python - perfringo - Jul-02-2019 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 * 7EDIT: this is useful read: What is the difference between arguments and parameters? RE: What am I doing wrong? New to Python - Stthmc_1995 - Jul-02-2019 That solved it. Looks like I was overthinking it. Thanks very much for the help guys. |