0

I want to know if there is no text string in the variables. And then compare if there is another text string. The variables results It may contain the following: yes, no or ? I have follow code:

internet = result1
vpn = result2
zetas = result3

values = [internet, vpn, zetas]

    if any(v !== "?" for v in values):
        print("No exist ?")
    if any(v == "no" for v in values):
        print("Exist a NO")
    else:
        print("Good")

especially to see the most elegant way of doing this.

2
  • 1
    Can you please use some examples to demonstrate what you're trying to do and what the expected output is? Commented Apr 5, 2019 at 21:23
  • 1
    It looks like you are looking for elif. And also, there is no !==. Use == or !=. Commented Apr 5, 2019 at 21:23

3 Answers 3

2

you can check value in list or value not in list

values = (internet, vpn, zetas)

if "?" not in values:
    print("No exist ?")
if "no"  in values:
    print("Exist a NO")
else:
    print("Good")
Sign up to request clarification or add additional context in comments.

Comments

1

First it is preferable to pass generator expressions instead of lists for loop feeding.

Secondly, although there can be many ways,, a good way would simply be:

values = (internet, vpn, zetas)
if "?" in (v for v in values):
  print("No exist ?")

Generator expressions save memory and time, not critical for the script in hand but useful for larger chunks of data.

5 Comments

instead of (v for v in values) you can use values - if "?" in values:
That's right! But this way it will prevent "?" inside strings from escaping detection, I thought it would be a nice feature!
(v for v in values) and values will work the same so there is no feature.
That's actually not correct. But you can try it. python values = ["qwerty", "asdf"] if "qwert" in values: print('1') if "qwert" in (v for v in values): print('2') You will see that it will print 2.
My python print nothing for both versions. And this is correct result because there is no "qwert" on list ["qwerty", "asdf"]. If I use if "qwerty" in both versions then I see 1 and 2.
0

I checked if the query you had regarding the question mark in inverted commas would escape detection using an array comparison [x in B for x in A]

#I assigned values to your variables to test
result1 ='?'
result2="no"
result3 = "yes"

internet = result1
vpn = result2
zetas = result3

#created an array with above values in it both in string format and as variable values (appended a number just for test)
A = [internet, 'no', '?', 'yes', vpn, zetas, '8']

#array of sample strings
B = ['?', 'no', 'yes']

exists = [x in B for x in A]
print (exists)

When the results of the comparison printed it gave: [True, True, True, True, True, True, False]. However if there were additional quote marks, e.g. '"?"' , the comparison on this printed as False. (Items in single quotes evaluate the same as double quotes..)

Feel free to run/re-assign values/test etc as you please. (might not be the answer you're looking for but hope it helps)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.