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

Why does this seem to ignore the if statements and just print the statements even if they are false?

So this program is supposed to ask for three numbers (R,G,B), that are larger than one and less than 255, and if they are larger than 255 or less than 0 it’s supposed to print "R/G/B number is incorrect", but if it is larger than 0 and less than 255, it’s supposed to output nothing. My problem is that no matter what number I input, it just prints the "R/G/B number is incorrect" phrase anyways.

Code (python)

r = input("Enter the red: ")
g = input("Enter the green: ")
b = input("Enter the blue: ")

if (r < 0 or r > 255):
    print("Red number is incorrect.")

if (g < 0 or g > 255):
    print("Green number is incorrect.")

if (b < 0 or b > 255):
    print("Blue number is incorrect.")

Some examples of output:

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

Enter the red: 20
Enter the green: 300
Enter the blue: -1
Red number is incorrect.
Green number is incorrect.
Blue number is incorrect.

or

Enter the red: 20
Enter the green: 20
Enter the blue: 20
Red number is incorrect.
Green number is incorrect.
Blue number is incorrect.

>Solution :

You are not converting them to numbers using g = int(input(...))

This should resolve the problem.

Note: in your example, it is comparing strings to numbers, which obviously doesn’t work.

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