Click here to Skip to main content
15,032,256 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
The manager of a building materials delivery service needs a program to check the contents and weight
of sacks to ensure that correct orders are made up for delivery. A price for the order will be calculated.

Write and test a program for the manager.
• Your program must include appropriate prompts for the entry of data.
• Error messages and other output need to be set out clearly.
• All variables, constants and other identifiers must have meaningful names.

You will need to complete these three tasks. Each task must be fully tested.
TASK 1 – Check the contents and weight of a single sack
Each sack must obey the following rules to be accepted:

• contain cement, gravel or sand, with a letter on the side for easy identification
o C - cement
o G - gravel
o S - sand

• sand or gravel must weigh over 49.9 and under 50.1 kilograms
• cement must weigh over 24.9 and under 25.1 kilograms

Input and store the weight and contents for one sack. The contents must be checked and an incorrect
sack rejected. The weight must be validated on entry and an overweight or underweight sack rejected.

Output the contents and weight of an accepted sack. If a sack is rejected, output the reason(s).

What I have tried:

Content = str(input("What is the content?"))

Content_sub1 = "C"

if Content_sub1 in Content :
    Weight_ofc = float(input("What is the weight of the sack"))
    if Weight_ofc < 49.9 :
            print("The weight of the sac is too low")
    elif Weight_ofc > 55 :
            print("The weight of the sac is too high")
    else: print(Content ,  Weight_ofc )
else : print("the sack is invalid")

Content_sub2 = "G"

if Content_sub2 in Content :
    Weight_ofg = float(input("What is the weight of the sack"))
    if Weight_ofg < 49.9 :
            print("The weight of the sac is too low")
    elif Weight_ofg > 55 :
            print("The weight of the sac is too high")
    else: print(Content ,  Weight_ofg )
else : print("the sack is invalid")

Conent_sub3 ="S"

if Conent_sub3 in Content :
    Weight_ofs = float(input("What is the weight of the sack"))
    if Weight_ofs < 24.9 :
            print("The weight of the sac is too low")
    elif Weight_ofs > 25.1 :
            print("The weight of the sac is too high")
    else: print(Content , Weight_ofs)
else : print("the sack is invalid")
Updated 17-Jan-21 22:43pm
Dave Kreskowiak 17-Jan-21 20:21pm
You never said anything about a problem you're having, nor expressed a question.
Richard MacCutchan 18-Jan-21 4:31am
Your weight values are incorrect.

We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
Dynam0 18-Jan-21 9:33am
@OriginalGriff, thank you so much for trying to help me, and thankyou for leading me to the correct path. You are right, i should try and do it myself then when it is a problem i cannot solve at all i can post it, also thanks for linking the page as i am going to use it more often
OriginalGriff 18-Jan-21 9:57am
You're welcome!
You could simplify your code considerably by writing a function to test the sack weight. Create a dictionary that contains the sack type as the key, and the weight limits as the value item. Something like:
sacks = { "C": [24.9, 25.1], "S": [49.9,50.1 ], "G": [49.9,50.1 ]}

You can then pass the type and weight of the sack to the function for checking. The function can then use the sack type to get the lower and upper weights from a list and do the comparisons that way.
Dynam0 18-Jan-21 11:22am
thankyou richard, i will implement that on my code and use it. Regards
Richard MacCutchan 18-Jan-21 11:27am
You are welcome. Here is a link to the Python documentation on Dictionary usage: Built-in Types — Python 3.7.9 documentation[^].
Dynam0 18-Jan-21 11:44am
Richard, what type of function can i use
Richard MacCutchan 18-Jan-21 11:51am
You must write the function. And it should accept two parameters, the letter that identifies the sack type, and the weight in kilograms. You can then iterate through the dictionary looking for a matching letter (don't forget to convert it to upper case). The value entry on the matching letter gives you the limits for the weight. So you just need to compare the given weight with the two values to see if it is in that range.
Dynam0 18-Jan-21 12:50pm
okay, let me research more on that, at the moment i am abit lost
Richard MacCutchan 18-Jan-21 12:58pm
Part of the problem in answering questions here, is that we do not know at what stage you are in your learning. So we have to make assumptions. If you have not learned about using functions, you can still make use of the dictionary structure. You just need to capture the input values and follow that with the code that does the validation.
Dynam0 18-Jan-21 13:28pm
Richard, thanks. I added a few things to the code to make it sensible and it was accepted by the teacher. Thankyou for directing me
Richard MacCutchan 19-Jan-21 3:32am
Well done, and good luck for the future.
Dynam0 19-Jan-21 6:47am
thanks, agian

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900