0

this program keeps printing the first if statement no matter what condition is met

def elements():
  element = input("Enter an element of choice > ")
  if element.lower() ==='li' or 'lithium':
    print("Element: Lithium\nAtomic weight: 6.94\nGroup: Alkali metals(1)")
  elif element.lower() == 'h' or 'hydrogen':
    print("Element: Hydrogen\nAtomic weight:1.007\nGroup: Alkali metals(1)")
  elif element == 'he' or 'helium':
    print("Element: Helium\nAtomic weight:4.002\nGroup: Noble Gases(0)")
    
elements()

Output:

Enter an element of choice > h
Element: Lithium
Atomic weight: 6.94
Group: Alkali metals(1)
kmillw23
  • 1
  • 1

1 Answers1

1

This is incorrect:

if element.lower() == 'li' or 'lithium':

It is logically equivalent to:

if 'lithium' or (element.lower() == 'li'):

That is, 'lithium' always evaluates as true since you're not comparing it to anything, so having it as an or statement will always cause this if to be true.

You want

if element.lower() == 'li' or element.lower() == 'lithium':

Or

if element.lower() in ['li', 'lithium']
Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102