python if elif else statement

Question:

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"
Asked By: sakefon

||

Answers:

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.

Answered By: Jeanne Boyarsky

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.

Answered By: Lie Ryan

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!

Answered By: aIKid
  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"
Answered By: Curry

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.

Answered By: Trevor Merrifield

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)
Answered By: 11swallowedinthesea

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"
Answered By: CasperTN

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')
Answered By: Biddrup Mallick

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)

Answered By: Jude Eliboh

Remove quotations from integers in if statements such as:

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

Answered By: user15271326
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.