1

I have this exercise , calculates the cost of sending a small parcel. Te post office charges R5 for the first 300g, and R2 for every 100g thereafter (rounded up), up to a maximum weight of 1000g .

weight = raw_input("What are the weight of you parcel: ")    

if weight <= 1000:
   if weight <= 300:
      cost = 5
     print("You parcel cost: " + cost)
   else:
      cost = 5 + 2 * round((weight - 300)/ 100)
      print("You parcel cost: " + cost)
else:
    print("Maximum weight for amall parcel exceeded.")
    print("Use large parcel service instead.")

When i execute the IDLE console , I come only the last else statements.

5 Answers 5

4

Cast weight to an int, weight = int(weight). Right now it's a string, which always evaluates to False when compared to 1000.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks , i have what you tell me , but when i write 350 , not come the First else statements ...
If your code is as written, you have a spurious indent on the first print statement.
My answer addresses the 350 problem @iratxe It correctly goes into the inner else statement. Your math calculation is incorrect, causing cost to only be 5
2

First, you've got indentation problems. Two, you are comparing strings to ints. Then, compare...

>>> (350 - 300) / 100
0
>>> (350 - 300) / float(100)
0.5

You should check this yourself, but round(0) = 0, and round(0.5) = 1.


Here's the code that should fix the problems

weight = int(raw_input("What are the weight of you parcel: "))

if weight <= 1000:
  if weight <= 300:
    cost = 5
  else:
    cost = 5 + 2 * round((weight - 300) / float(100))
  print("Your parcel cost: {}".format(cost))
else:
  print("Maximum weight for small parcel exceeded.")
  print("Use large parcel service instead.")

5 Comments

Very Thanks , you code answer my question , but i think that you forget :
sorry... you forget : print("You parcel cost: " + str(cost)) , in if statement ..., now all it is veryyyyyyyy gut , thanks
No I didn't forget. You don't need two print statements - just print after and outside the else
You have to be more specific and finish your comments. I can't test all numbers, but 350 displays 7 and 300 displays 5. repl.it/EjQN/0
sorry ... , now a have copy and paste you code and all is GUT, sorry i have only my code corrective....
1

weight becomes a string type on line 1, then in the if statement you compare weight to an int. Fix this by converting the user input to int

Change your first line to:

weight = int(raw_input("What are the weight of you parcel: "))

Also if you are using python3 I would change raw_input to input

5 Comments

Isn't a string and int comparison supposed to raise a TypeError?
@lucasnadalutti thought the same but apparently in python3 only.
Thanks for your answer , I making this , and now i come : cannot concatenate 'str' and 'float' objects
On this line print("You parcel cost: " + cost) you try to bring a string and float together, you need to convert the cost to a string like this: print("You parcel cost: " + str(cost))
Whit int is gut only with 300, when I write 350 i come : cannot concatenate 'str' and 'float' objects
0

float(Use input() instead of raw_input() for integers and don't try to concatenate strings and integers.

The following code works:

weight = input("What are the weight of you parcel: enter code here")

if weight <= 1000:
    if weight <= 300:
        cost = 5
        print("You parcel cost: " + str(cost))
    else:
        cost = 5 + 2 * round((weight - 300)/ float(100))
        print("You parcel cost: " + str(cost))

else:
    print("Maximum weight for amall parcel exceeded.")
    print("Use large parcel service instead.")

5 Comments

Even on Python 2.7? I am not familiar with 2.7 but I thought input would try to execute a Python code
Yes, when i write 350 , i come : cannot concatenate 'str' and 'float' objects
The question was tagged as python27
Why when i write 350 , not come the First else statements ??
Yes, in 2.7 using input evaluates it.
0

Cast variable weight to a float when doing mathematics operation by

weight=float(input())

It will solve all the problems.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.