nikki
nikki

Reputation: 353

How do I correctly write this if statement?

Suppose that I get the values for valid, d ,sn from a function that I introduced in my program. Then I pass it through the following if statement in python to output the result.

I want the program to print the following statements:

  1. print on if the sn >= 0 and 1<d <90.
  2. print off-right if the sn<0.
  3. print off-left if the d>=90
  4. other than that, for anything that is not valid outputs unknown.

Here is what I wrote, how do I include d when checking the statement?

if valid:
    if sn >= 0 and 1<d <90:
        print(" on ")        
    else:
        print("off")
else:
    print("unknown")

Upvotes: 0

Views: 83

Answers (2)

OldManSeph
OldManSeph

Reputation: 2719

if valid:
   if sn >= 0 and d > 1 and d < 90:
      print(" on ")
   elif sn < 0:
      print("off-right")
   elif d >= 90:
      print("off-left")
else:
   print("unknown")

Upvotes: 4

Daniyal Shaikh
Daniyal Shaikh

Reputation: 418

Not sure what you want. But most probably you want to do this i guess?


if valid:
    if sn >= 0 and (d>1 and d<90):
        print(" on ")
        
    else:
        if sn<0:
            print("off-right")
        elif d>=90:
            print('off-left')
else:
    print("unknown")

This is a self explanatory answer.. If you get confused by 1<d<90 then you can use what i did above

Upvotes: 3

Related Questions