-3

I want to compare one variable value which is a string with an another string value in a IF statement using a python code.

Expectation:

Variable Name and Value:: v1='purchased' String Value:: string='purchase'

If both (v1 and string) are not equal then,

print "Both strings are not equal".

Outcome:

v1 = 'purchased'

if (v1 not like 'pur%') or (v1 != 'ordered'):
    print("Both strings are not equal", v1)  # return if true
else:
    print("Both strings are equal", v1)  # return if false

Scenario 1: While passing v1 as "purchase" getting "Both strings are not equal". It should give me "Both strings are equal". As I am trying to match it partially.

Scenario 2: When giving "ordered" it is giving same "Both strings are not equal". It should give me "Both strings are equal".

  • 1
    I don't understand the question; did you mean to use `and` or do you want to achieve something different? – B Remmelzwaal Feb 16 '23 at 14:58
  • 1
    to compare a variable against different values don't use `and`, use `in` instead: `if v1 in ("purchased", "ordered"):` – Sembei Norimaki Feb 16 '23 at 15:01
  • Here, if I pass purchased in V1 it is coming inside the if statement and saying "Both strings are not equal". Which should not be the right one. – user3040157 Feb 16 '23 at 15:01
  • 4
    your if-condition is always true. The only way how the else branch would be reached is if "v1" is both equal to 'purchased' AND 'ordered', which is not possible. – Simon Feb 16 '23 at 15:02
  • 2
    The way to do it is **not to use** `or`, because that is **logically** incorrect. This isn't a programming question, but a reasoning one. Please see the linked duplicate for more details. – Karl Knechtel Feb 16 '23 at 19:37

4 Answers4

-1

With OR and different you'll always end up in if case. To compare strings change to the following:

v1 = 'purchase'

if v1 != 'purchased' and v1 != 'ordered':
    print("Both strings are not equal", v1)  # return if true
else:
    print("Both strings are equal", v1)  # return if false

an other way is to use "in" operator like this:

v1 = 'purchase'
    
if v1 not in ['purchased', 'ordered']:
    print("Both strings are not equal", v1)  # return if true
else:
    print("Both strings are equal", v1)  # return if false
s_frix
  • 323
  • 2
  • 11
-1

You should use and statement, in your case if v1 == 'purchased' then v1 != 'ordered' so the if condition is satisfied.

if v1 == 'ordered' then v1 != 'purchased so the if condition is satisfied too.

try (v1 != 'purchased') and (v1 != 'ordered')

or (v1 == 'purchased') or (v1 == 'ordered')

Berni
  • 36
  • 6
-1

i think the error comes from your ().

Try this instead:

v1 = 'ordered'

if v1 == 'purchased' or v1 == 'ordered':
    print(f'Both Strings are equal {v1}')
else:
    print(f'Both Strings are not equal {v1}')  

and i also used f strings for readability.

Hope i helped.

  • No, the parenthesis are not a problem. On the other hand you made other changes that you didn't even explain... – Tomerikoo Feb 16 '23 at 15:07
-1
if string1 == "value1" or string2 == "value2":
    Do Something
else:
    Do something else
gabrsafwat
  • 108
  • 7