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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Joshua Billings
    New Member
    • Mar 2011
    • 1

    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.

    Code:
    if ad > 6:
        rectAD.setFill("red")
    if ad <= 3:
        rectAD.setFill("red")
    else:
        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?
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    Another way to code this is to use green as the default and eliminate the else statement.
    Code:
    rectAD.setFill("green")
    if (ad <=3) or (ad > 6):
        rectAD.setFill("red")

    Comment

    Working...