Search code examples
pythonif-statementnested

python if elif else statement


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"

Solution

    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"