By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,965 Members | 1,962 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,965 IT Pros & Developers. It's quick & easy.

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

P: 1
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") 

Share this Question
Share on Google+
1 Reply


Expert 100+
P: 624
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.