21

I'm trying to create a program with python that calculate the cost for shipping.

However, I can't run the program to where it works properly.

What ever my total is the same amount comes out as $6 for US and $8 for Canada. I can't seem to get pass that.

total = raw_input('What is the total amount for your online shopping?')
country = raw_input('Shipping within the US or Canada?')

if country == "US":
    if total <= "50":
        print "Shipping Costs $6.00"
    elif total <= "100":
            print "Shipping Costs $9.00"
    elif total <= "150":
            print "Shipping Costs $12.00"
    else:
        print "FREE"

if country == "Canada":
    if total <= "50":
        print "Shipping Costs $8.00"
    elif total <= "100":
        print "Shipping Costs $12.00"
    elif total <= "150":
        print "Shipping Costs $15.00"
    else:
        print "FREE"
Alex Kulinkovich
  • 4,408
  • 15
  • 46
  • 50
sakefon
  • 229
  • 1
  • 2
  • 4
  • You need to format your code correctly... try indenting it 4 spaces. – Greg Oct 15 '13 at 00:59
  • 6
    Is it intentional that shipping is free if the purchase is greater than $150? – SethMMorton Oct 15 '13 at 01:07
  • This question is terrible and I don't understand why it got 100k views and a positive reception. The problem was blatantly misidentified, the question title is misleading as a result, and no attempt was made to create a proper [mre] nor to [debug](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) the code independently for a simple debugging question. It *could possible have been* a canonical for "how do I compare strings that represent numbers?"; but it would never be found by someone who needs that information. The linked duplicate is the closest we have as a proper question. – Karl Knechtel Sep 25 '22 at 21:44

10 Answers10

23
  1. you should get integer from raw_input, not string. use int().
  2. comparison values like 50, 100, 150, ... also should be integer.

below is fixed code.

total = int(raw_input('What is the total amount for your online shopping?'))
country = raw_input('Shipping within the US or Canada?')

if country == "US":
    if total <= 50:
        print "Shipping Costs $6.00"
    elif total <= 100:
        print "Shipping Costs $9.00"   # improved indentation
    elif total <= 150:
        print "Shipping Costs $12.00"  # improved indentation
    else:
        print "FREE"

if country == "Canada":
    if total <= 50:
        print "Shipping Costs $8.00"
    elif total <= 100:
        print "Shipping Costs $12.00"
    elif total <= 150:
        print "Shipping Costs $15.00"
    else:
        print "FREE"
AcK
  • 2,063
  • 2
  • 20
  • 27
Curry
  • 885
  • 5
  • 15
12

You can't compare Strings numerically. Instead convert to an int first and then compare.

For example:

if int(total) < 50

Variables to avoid duplication would help too.

Jeanne Boyarsky
  • 12,156
  • 2
  • 49
  • 59
  • .. `int("5")` seems like a verbose way to spell `5`. – DSM Oct 15 '13 at 01:01
  • Well yes. But in the real code, total is a variable so int(total) is needed. Ah you are saying the point is it should be if int(total) < 50. I'll edit for clarity. – Jeanne Boyarsky Oct 15 '13 at 01:02
  • I actually think it would make more sense to convert `total` to an `int` (or, IMHO, a `float`) once, rather than repeating the conversion for each comparison. – DSM Oct 15 '13 at 01:04
  • So do I. Hence my comment about variables to avoid duplication. – Jeanne Boyarsky Oct 15 '13 at 01:05
7

You are comparing strings numerically. That's impossible, like comparing apple with orange, which one is bigger? The computer won't understand that, it needs to compare the size.

To do that, we need to convert it to an integer. Use the int() function. Here:

#convert it to an integer straight away
total = int(raw_input('What is the total amount for your online shopping?')) 
country = raw_input('Shipping within the US or Canada?')

if country == "US":
    if total <= 50:
        print "Shipping Costs $6.00"
    elif total <= 100:
            print "Shipping Costs $9.00"
    elif total <= 150:
            print "Shipping Costs $12.00"
    else:
        print "FREE"

if country == "Canada":
    if total <= 50:
        print "Shipping Costs $8.00"
    elif total <= 100:
        print "Shipping Costs $12.00"
    elif total <= 150:
        print "Shipping Costs $15.00"
    else:
        print "FREE"

Hope this helps!

aIKid
  • 26,968
  • 4
  • 39
  • 65
7

When you compare strings it does so lexicographically, like in a phone book. For example:

"a" < "b": True
"bill" < "bob": True
"100" < "3": True

If you want to compare numbers in the order that we count them you need to use the int type.

total = int(raw_input('What is the total amount for your online shopping?'))

Then change all of the string literals in your code like "50" to integer literals like 50.

Trevor Merrifield
  • 4,541
  • 2
  • 21
  • 24
4

This:

total = raw_input('What is the total amount for your online shopping?')

produces a string. Comparison between string and numbers are not very well defined. You need to convert total to a number first. Example:

total = int(raw_input('What is the total amount for your online shopping?'))

(this ignores input error handling such as when the user's input is not a number)

Note that the behavior changes in Python 2.x and Python 3.x. In Python 2.x:

Objects of different types, except different numeric types and different string types, never compare equal; such objects are ordered consistently but arbitrarily (so that sorting a heterogeneous array yields a consistent result).

...

CPython implementation detail: Objects of different types except numbers are ordered by their type names; objects of the same types that don’t support proper comparison are ordered by their address.

while in Python 3.x:

Objects of different types, except different numeric types, never compare equal.

Lie Ryan
  • 62,238
  • 13
  • 100
  • 144
4

When using raw_input your user input comes in as a string and you cannot calculate numbers in the format of strings. So you need to change your string input to an integer in order to make the comparisons. You can do like this:

total = int(raw_input('What is the total amount for your online shopping?'))
country = raw_input('Shipping within the US or Canada?')

if country == "US":
    if total <= 50:
        print "Shipping Costs $6.00"
    elif total <= 100:
        print "Shipping Costs $9.00"
    elif total <= 150:
        print "Shipping Costs $12.00"
else:
    print "FREE"

elif country == "Canada":
    if total <= 50:
        print "Shipping Costs $8.00"
    elif total <= 100:
        print "Shipping Costs $12.00"
    elif total <= 150:
        print "Shipping Costs $15.00"
    else:
        print "FREE"

else:
    print "Try Again"
CasperTN
  • 441
  • 1
  • 5
  • 14
3

It's like adding apples & houses to get the total which is impossible. It needs to be the same type, in this case integer type, to get the total. Use the int() to convert the string into an integer.

 total = int(raw_input('What is the total amount for your online shopping?'))

could also be (but less preferable):

 total = raw_input('What is the total amount for your online shopping?')
 total = int(total)
2

input returns a string

if total is supposed to return an input for math operations then you should float the input

total = (raw_input('What is the total amount for your online shopping?')) total = float(total)

1

Remove quotations from integers in if statements such as:

if total <= "50" -------> if total <= 50

-1

I am just a fresher here and python programming. I was trying to solve your problem. Hope, this one could help you.

if country == 'US':
if total <= 50:
    print ('Shipping Costs $6.00')
elif total <= 100:
        print ('Shipping Costs $9.00')
elif total <= 150:
        print ('Shipping Costs $12.00')
else:
    print ('FREE')

elif country == 'Canada':
if total <= 50:
    print ('Shipping Costs $8.00')
elif total <= 100:
    print ('Shipping Costs $12.00')
elif total <= 150:
    print ('Shipping Costs $15.00')
else:
    print ('FREE')

else:
print ('Country name is case sensetive so do it perfectly')
  • Point out what you have changed. In case of Python, get the indentation right, or the program is will be wrong just because of that. Also, in this case, you have only changed the `print` lines as it seems which might help depending on the python version but is not an answer to the problem. – Risadinha Oct 12 '16 at 14:54