Posts: 256
Threads: 60
Joined: Apr 2019
May-02-2022, 10:17 AM
(This post was last modified: May-02-2022, 11:52 AM by paul18fr.)
Hi
Sorry for that basic question, but I do not understand why the answer is always "no" when using simple quote between Y: where I'm missing something?
Thanks
Paul
MyList = ["ANSWER='Y'"]
if ("ANSWER=Y" or "ANSWER='Y'" or "ANSWER=\'Y\'") in MyList:
print("Answer = Yes")
else:
print("Answer = No") Output: Answer = No
Posts: 7,081
Threads: 122
Joined: Sep 2016
May-02-2022, 10:35 AM
(This post was last modified: May-02-2022, 10:35 AM by snippsat.)
Look at Multiple expressions with "or" keyword.
MyList = ["ANSWER='Y'"]
if MyList[0] in ("ANSWER=Y", "ANSWER='Y'", "ANSWER=\'Y\'"):
print(f'<{MyList[0]}> is in list') Output: <ANSWER='Y'> is in list
Also is a little ugly that need find a string with = that has no meaning now as is just a string.
Posts: 1,822
Threads: 2
Joined: Apr 2017
Note that non-empty strings evaluate to True when asked for their truth value.
Posts: 4,489
Threads: 69
Joined: Jan 2018
May-02-2022, 11:13 AM
(This post was last modified: May-02-2022, 11:13 AM by Gribouillis.)
A sequence of «or» separated items evaluates to the first «non false» item, otherwise to the last item
>>> "ANSWER=Y" or "ANSWER='Y'" or "ANSWER=\'Y\'"
'ANSWER=Y'
>>> Similarly a sequence of «and» seperated items evaluates to the first «false» item, otherwise to the last item.
jefsummers likes this post
Posts: 256
Threads: 60
Joined: Apr 2019
Ok I figured out my mistake
Thanks
Posts: 1,345
Threads: 2
Joined: May 2019
(May-02-2022, 10:17 AM)paul18fr Wrote: Hi
Sorry for that basic question, but I do not understand why the answer is always "no" when using simple quote between Y: where I'm missing something?
Thanks
Paul
What are you trying to do? This seems an odd construct.
Is this closer to what you want (just guessing)
MyList = ["Y", "y"]
if answer in MyList:
print("Answer = Yes")
else:
print("Answer = No")
Posts: 256
Threads: 60
Joined: Apr 2019
I've been using such kind of snippet in conjunction with "sys.argv"; as mentioned in previous post, my code was wrong, and I modified it. Now it works as expected.
Thanks to all
MyList = ["ANSWER='Y'"]
if ("ANSWER=Y" in MyList) or ("ANSWER='Y'" in MyList):
print("Answer = Yes")
else:
print("Answer = No")
Posts: 2,015
Threads: 9
Joined: May 2017
You could use ArgumentParser , which returns a Namespace , where you can access for example to answer.
from argparse import ArgumentParser
ANSWERS = ("n", "y")
def get_args():
parser = ArgumentParser()
parser.add_argument("--answer", dest="ANSWER", metavar="YOUR_CHOICE", default="n", choices=ANSWERS)
return parser.parse_args()
if __name__ == "__main__":
print(get_args()) I named the script c.py:
Quote:[andre@andre-Fujitsu-i5 ~]$ python c.py -h
usage: c.py [-h] [--answer YOUR_CHOICE]
options:
-h, --help show this help message and exit
--answer YOUR_CHOICE
[andre@andre-Fujitsu-i5 ~]$ python c.py --answer y
Namespace(ANSWER='y')
[andre@andre-Fujitsu-i5 ~]$ python c.py --answer i
usage: c.py [-h] [--answer YOUR_CHOICE]
c.py: error: argument --answer: invalid choice: 'i' (choose from 'y', 'n')
Also, arguments with more than one parameter (nargs) are possible.
|