python if elif else statement

python if statement multiple conditions
python if statement string
python if-else one line
python if-elif without else
if else python 3
python elif invalid syntax
python statements
python if elif else: return

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"

  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"

Python if, ifelse, ifelifelse and Nested if Statement, Besides the while statement just introduced, Python uses the usual flow The keyword ' elif ' is short for 'else if', and is useful to avoid excessive indentation. The "else" MUST be preceded by an if test and will ONLY run when condition of the if statement is NOT met. else will run if all others fail. If you only have two choices in your construction, use if ..else If there are more than two options, use if ..elif ..else.. that will make it easier to read elif is short for "else if" Conditional Tests An If statement sets an condition with one or more if statement that will be used when a condition is met.


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.

Python if elif else, In the previous tutorials we have seen if statement and if..else statement. In this tutorial, we will learn if elif else statement in Python. The. In Python, you have the if, elif and the else statements for this purpose. In this tutorial, you will work with an example to learn about the simple if statement and gradually move on to if-else and then the if-elif-else statements. You will also learn about nesting and see an nested if example.


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.

4. More Control Flow Tools, Python IF, ELIF, and ELSE Statements. In this tutorial, you will learn exclusively about Python if else statements. While writing code in  Python if-Elif-Else Statement The first three if-else constructs can only address two outcomes, i.e., True or False. However, the expression next to “if” can also evaluate to a value different from the boolean. It means to have more conditions, not just a single “else” block.


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!

Python If elif else statement example, An If statement sets an condition with one or more if statement that will be used when a condition is met. There can be zero or more elif parts, and  Python if-elif-else statement is used to write a conditional flow code. The statements are in the order of if…elif…else. The ‘elif’ is short for ‘else if’ statement. It’s shortened to reduce excessive indentation.


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.

Python IF, ELIF, and ELSE Statements, Python - Decision Control: if, else, elif. By default, statements in the script are executed sequentially from the first to the last. If the processing logic requires so,​  If-Elif-Else Statement It checks the if statement condition. If that is false, the elif statement is evaluated. In case the elif condition is false, the else statement is evaluated.


Python IfElifElse Statement, The code options.get(something, doThisMostOfTheTime)() looks like it ought to be faster, but it's actually slower than the if elif else construct, because it  The if-elif-else statement is used to conditionally execute a statement or a block of statements. Conditions can be true or false, execute one thing when the condition is true, something else when the condition is false.


Decision Control in Python - if, else, elif, This tutorial explains the syntax and use of Python if else, if-elif-else, nested if, one line if-else conditional/decision making statements. The if…elif…else statement is used in Python for decision making. Python if Statement Syntax if test expression: statement (s) Here, the program evaluates the test expression and will execute statement (s) only if the text expression is True.


Most efficient way of making an if-elif-elif-else statement when the , An else statement can be combined with an if statement. An else statement contains a block of code that executes if the conditional expression in the if statement resolves to 0 or a FALSE value. The else statement is an optional statement and there could be at the most only one else statement following if .