Python Forum
What am I doing wrong? New to Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What am I doing wrong? New to Python
#1
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.
Reply
#2
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
Reply
#3
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.
Reply
#4
When I print total weight, it says 2016. Also return total_weight is the correct way to return.
Reply
#5
The sum is now working correctly but '
return total_weight
' is still presenting the error 'return' outside function
Reply
#6
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?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
That solved it. Looks like I was overthinking it. Thanks very much for the help guys.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,559 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  python gives wrong string length and wrong character thienson30 2 3,002 Oct-15-2019, 08:54 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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