Python Forum
[solved] Basic question on list matchiing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[solved] Basic question on list matchiing
#1
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
Reply
#2
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.
Reply
#3
Note that non-empty strings evaluate to True when asked for their truth value.
Reply
#4
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
Reply
#5
Ok I figured out my mistake
Thanks
Reply
#6
(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")
Reply
#7
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")
Reply
#8
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [solved] list content check paul18fr 6 682 Jan-04-2024, 11:32 AM
Last Post: deanhystad
  Basic Coding Question: Exit Program Command? RockBlok 3 554 Nov-19-2023, 06:31 PM
Last Post: deanhystad
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,369 Mar-24-2023, 08:34 AM
Last Post: fullytotal
  Loop through list of ip-addresses [SOLVED] AlphaInc 7 3,956 May-11-2022, 02:23 PM
Last Post: menator01
  [Solved] Novice question to OOP: can a method of class A access attributes of class B BigMan 1 1,307 Mar-14-2022, 11:21 PM
Last Post: deanhystad
  Very basic calculator question BoudewijnFunke 4 1,933 Dec-10-2021, 10:39 AM
Last Post: BoudewijnFunke
  [solved] Sort list paul18fr 5 2,857 Aug-18-2021, 06:34 AM
Last Post: naughtyCat
  basic question isinstance tames 5 2,828 Nov-23-2020, 07:20 AM
Last Post: tames
  basic question about tuples and immutability sudonym3 6 2,897 Oct-18-2020, 05:11 PM
Last Post: sudonym3
  List index out of range error when attempting to make a basic shift code djwilson0495 4 2,996 Aug-16-2020, 08:56 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020