-2

The problem is simple A string like = '932152' should start with digits ( 9 or 8 or 7 ) so I used this code:

    p ='932152'
    if((p[0] !='7') or (p[0] != '8') or (p[0] != '9')):
        print'NO'
    else:
        print'YES'

and I get 'NO'

Why is that? I tried each condition alone with putting it in a single if statement and it obviously working.

7 Answers7

1
p ='932152'
if((p[0] == '7') or (p[0] == '8') or (p[0] == '9')):
    print('YES')
else:
    print('NO')
Dawid Rutkowski
  • 410
  • 3
  • 8
1

In this case p[0] is '9'. So let’s look at what that means for the code:

if(('9' !='7') or ('9' != '8') or ('9' != '9')):
    print'NO'
else:
    print'YES'

As it turns out, '9' does not equal '7' or '8', so the first two != clauses are true. True or True or False equals True.

This will be true whatever p[0] is; it will always be unequal to at least one of those*. What you want is and, not or.

* It is technically possible to define something which is equal to all of those, but it’s more advanced and you shouldn’t run into something like that in general unless you’re deliberately trying to break the equality operator.

Daniel H
  • 7,223
  • 2
  • 26
  • 41
0

For or statements, only one condition of those listed has to be true for your "NO" to print. You need to use and. Because you want to make sure that the value is not any of those 3 numbers before printing "NO".

Joshua Hall
  • 332
  • 4
  • 15
0

I guess you missed the basic concept of using 'or'. In your if statement, you are using 3 conditions:

   1)p[0] !='7',  True as '9' is not = to '7';
   2)p[0] !='8', True as '9' is not = to '8';
   3)p[0] !=9, False as '9' is equal to '9'

Hence your final if statement becomes--->if True or True or False & therefore prints 'NO'. follow the below code for your desired results:

p ='932152'
if((p[0] == '7') or (p[0] == '8') or (p[0] == '9')):
     print('YES')
else:
     print('NO')
Mehul Gupta
  • 1,829
  • 3
  • 17
  • 33
0

Fundamentally this looks like an issue with your understanding of Boolean logic.

Breaking it down you're performing three tests:

  1. p[0] != '7' >>> True
  2. p[0] != '8' >>> True
  3. p[0] != '9' >>> False

BUT You're testing this with an 'or' condition and True OR True Or False, still evaluates to True.

You have two options, change your test to: if((p[0] !='7') and (p[0] != '8') and (p[0] != '9'))

OR test for: if not((p[0] =='7') or (p[0] == '8') or (p[0] == '9')):

0

Your Condition will always be true. Instead of using 'or' try this may be it can help you.

p ='732152'
num_list = ['9','8','7']
if p[0] in num_list:
    print('YES')
else:
    print("No")
Mannya
  • 125
  • 1
  • 2
  • 12
0

The condition which u wrote should be reversed since you told that the program should accept strings starting with 9 or 8 or 7.

If you wanted to use the negation then you'll have to go for

if((p[0]!='7') and (p[0]!='8') and (p[0] != '9')):
    print("NO")
else:
    print("YES")

You could also go with the below code to make your conditions simpler

print("YES" if(p[0]=='9' or p[0]=='8' or p[0]=='7') else "NO")