Skip to main content
Chemistry LibreTexts

10.4: if-elif-else Statement

  • Page ID
    470659
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    An if-elif-else statement has two parts.  The if-elif and the else. For each if or elif condition a block of code is executed if it is true, and the next condition is then checked if it is false. If none of the condition are true, the else block of code is executed.  So if and elif execute when a condition is true, while else executes if it is false.  Note the conditions are tested sequentially, and sometimes this can effect the logic.

    clipboard_e33b9d5e178a1f6e39f8f48d6467f7d4b.pngFigure \(\PageIndex{1}\): Copy and Paste Caption here. (Copyright; author via source)

    The following code uses the List content type (section 2.7) and the membership operator (section 2.18). These lists are lists of strings (section 2.6) and you are seeing which list the element is in.

    Caution

    The following code only works if you capitalize the first letter of each element, which is a formatting issue (Boron works, boron does not).  Note that element is a string, and we are using the membership to see if it is in a list of strings.  Can you find a string method that can fix this problem so that boron will work?  There are actually several methods to do this. So, change the code and run it.  Here is a hint, you can reassign a variable name to the same variable name with a method operating on it (var1=var1.method), where var1 is the variable name and method is the method you are using.

    element = input("Enter the element you want to know the family of: ")
    
    if element == "Hydrogen":
        print("Hydrogen belongs to the nonmetal family.")
    elif element in ["Lithium", "Sodium", "Potassium", "Rubidium", "Cesium", "Francium"]:
        print(f"{element} belongs to the alkali metal family.")
    elif element in ["Beryllium", "Magnesium", "Calcium", "Strontium", "Barium", "Radium"]:
        print(f"{element} belongs to the alkaline earth metal family.")
    elif element in ["Boron", "Aluminum", "Gallium", "Indium", "Thallium"]:
        print(f"{element} belongs to the metalloid family.")
    elif element in ["Carbon", "Silicon", "Germanium", "Tin", "Lead"]:
        print(f"{element} belongs to the nonmetal family.")
    elif element in ["Nitrogen", "Phosphorus", "Arsenic", "Antimony", "Bismuth"]:
        print(f"{element} belongs to the pnictogen family.")
    elif element in ["Oxygen", "Sulfur", "Selenium", "Tellurium", "Polonium"]:
        print(f"{element} belongs to the chalcogen family.")
    elif element in ["Fluorine", "Chlorine", "Bromine", "Iodine", "Astatine"]:
        print(f"{element} belongs to the halogen family.")
    elif element in ["Helium", "Neon", "Argon", "Krypton", "Xenon", "Radon"]:
        print(f"{element} belongs to the noble gas family.")
    elif elment in ["Cerium", "Praseodymium", "Neodymium", "Promethium", "Samarium", 
                   "Europium", "Gadolinium", "Terbium", "Dysprosium", "Holmium", 
                   "Erbium", "Thulium", "Ytterbium", "Lutetium"]:
        print(f'{element} is a lanthanide')
    elif element in ["Thorium", "Protactinium", "Uranium", "Neptunium", "Plutonium", 
                 "Americium", "Curium", "Berkelium", "Californium", "Einsteinium", 
                 "Fermium", "Mendelevium", "Nobelium", "Lawrencium", "Rutherfordium", 
                 "Dubnium", "Seaborgium", "Bohrium", "Hassium", "Meitnerium", "Darmstadtium", 
                 "Roentgenium", "Copernicium", "Nihonium", "Flerovium", "Moscovium", "Livermorium", "Tennessine", "Oganesson"]:
        print(f'{element} is an actinide')
    else:
        print(f'element is a transition metal')
    Hello world!

     


    This page titled 10.4: if-elif-else Statement is shared under a not declared license and was authored, remixed, and/or curated by Robert Belford.

    • Was this article helpful?