Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python if/elif statement not working correctly

Please help understand what is wrong with the code below. The code works fine if I pass values up to 34. Once I pass 35 or higher, the output is incorrect.

tuk=0
if tuk <= 24:
    print ('The text is very easy to read.')
elif tuk >= 25 & tuk <= 34:
    print('The text is easy to read.')    
elif tuk >= 35 & tuk <= 44:
    print('The text is moderately difficult to read.')
elif tuk >= 45 & tuk <= 54:
    print('The text is difficult to read')
elif tuk >= 55:
    print('The text is very difficult to read')
else:
    print('This is beyond')

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Your code is correct, but changing the "&" to "and" because "&" is a bitwise logical operator, not a conditional one.

For example:

z = 0 := 0b000
a = 1 := 0b001
b = 2 := 0b010
c = 3 := 0b011
d = 4 := 0b100 

now the result of the expression (a & c) is 0b001 or 1
but in conditional expression (a and b) the result will be true, which I suppose you are interested in.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading