3

This is code to find details about elements by asking the user about which element they want to learn about. The problem is when I run it it prints all of the print statements.

print ('Please type the element number or the name - no caps')
element = input('What element do you want to learn about?')


if element == ('1') or ('hydrogen'):
        print ('Hydrogen #1')
        print ('Gas')
        print ('Non-Metal')
        print ('Weight: 1.008')


if element == ('2') or ('helium'):
    print ('Helium #2')
    print ('Gas')
    print ('Non-Metal')
    print ('Weight: 4.0026')

if element == ('3') or ('lithium'):
    print ('Helium #3')
    print ('Solid')
    print ('Metal')
    print ('Weight: 6.94')

This is what happens when I run it.

Please type the element number or the name - no caps
What element do you want to learn about? 1
Hydrogen #1
Gas
Non-Metal
Weight: 1.008
Helium #2
Gas
Non-Metal
Weight: 4.0026
Helium #3
Solid
Metal
Weight: 6.94
CosmoCrash
  • 61
  • 1
  • 7

1 Answers1

5

That is because the test

 element == ('1') or ('hydrogen')

is interpreted as

 element == ('1')
  or
('hydrogen')

The second part is always true.

What you presumably intend is

if  element == '1' or element == 'hydrogen':
wallyk
  • 56,922
  • 16
  • 83
  • 148