469,282 Members | 1,822 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

Why is python program running the wrong part of if else statement?

I am working on a programming assignment, and I can't figure out what is wrong with these lines of code.

Expand|Select|Wrap|Line Numbers
  1. if ad > 6:
  2.     rectAD.setFill("red")
  3. if ad <= 3:
  4.     rectAD.setFill("red")
  5. else:
  6.     rectAD.setFill("green")
With the values I am using for testing, ad = 7. The problem is that the program is running the "else" statement, when I really would rather it didn't. What have I done wrong? Also, is there some way to consolidate the two "if" statements into one?
Mar 10 '11 #1

✓ answered by dwblas

Another way to code this is to use green as the default and eliminate the else statement.
Expand|Select|Wrap|Line Numbers
  1. rectAD.setFill("green")
  2. if (ad <=3) or (ad > 6):
  3.     rectAD.setFill("red") 

1 1521
dwblas
626 Expert 512MB
Another way to code this is to use green as the default and eliminate the else statement.
Expand|Select|Wrap|Line Numbers
  1. rectAD.setFill("green")
  2. if (ad <=3) or (ad > 6):
  3.     rectAD.setFill("red") 
Mar 10 '11 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by ed | last post: by
2 posts views Thread by Stu | last post: by
1 post views Thread by Alexnb | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.