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
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.
Note that non-empty strings evaluate to True
when asked for their truth value.
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.
Ok I figured out my mistake
Thanks
(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")
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")
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.