If the statement is false, but the code in it is still executing, as well as all the code in the entire script in python - New to Python

This is the code for finding details about items, asking the user which item they want to know. The problem is when I run it, it prints all the print statements. In the meantime, there is no need to worry about it. ”

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

      

+3


source to share


1 answer


This is because the test

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

      

interpreted as

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

      



The second part is always correct.

What you presumably assume is

if  element == '1' or element == 'hydrogen':

      

+5


source







All Articles