Python Forum
An IF statement with a List variable - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: An IF statement with a List variable (/thread-34228.html)



An IF statement with a List variable - dedesssse - Jul-08-2021

I'm trying to make it so if i enter a "Woman, girl or lady it will do the if statement

isfemale = ["woman", "girl", "lady", ]
isfemale = input("Enter your Gender: ")
if isfemale == [0,1,2]:
    print("You are a Woman!")



RE: An IF statement with a List variable - Gribouillis - Jul-08-2021

Try this
answer = input('Enter your gender: ').strip()
if answer in {'woman', 'girl', 'lady'}:
    print('You are a woman')



RE: An IF statement with a List variable - deanhystad - Jul-08-2021

In your example "isfemale" is doing too many jobs. First you use it to reference a list. In the next line you use it to reference a string entered by the user. And in the third line you compare it to a list. I've heard women are great multitaskers, but this is getting ridiculous.

You condition "if isfemale == [0,1,2]:" will never be True because isfemale references a string, and a string will never equal a list. It will also never be True because it is nonsense. what is [0,1,2]? Are you trying to do this?
isfemale = ["woman", "girl", "lady"]
gender = input("Enter your Gender: ")
if gender == isfemale[0] or gender == isfemale[1] or gender == isfemale[2]:
    print("You are a Woman!")
This code works because isfemale remains referencing ["woman", "girl", "lady"] instead of trying to do a job better done by two variables (isfemale and gender). It also works because it properly indexes the list to get the list values. You want to know if if gender is one of the VALUES in the isfemale list.

This works, but it is horrible. Checking if an item is in a dictionary or in a set of items is a common Python programming task. Griboullis solves this problem using a set. The curly brackets in {'woman', 'girl', 'lady'} tell Python to create a set that contains strings 'women', 'girl' and 'lady'. You could also save the values in a list (square brackets []) or a tuple (parenthesis ()), but a set is most efficient when testing if it contains an item.

This code:
if answer in {'woman', 'girl', 'lady'}:
tests if answer matches one of the strings in the set. It returns True if answer is 'women', or 'girl' or 'lady' an False otherwise. For example, typing 'Lady' will result in this conditional being False because 'Lady' != 'lady'.


RE: An IF statement with a List variable - perfringo - Jul-08-2021

Not english speaker but is a girl really a woman ('You are a Woman')? I would guess that common nominator for girl, woman and lady is gender i.e. female ('Your are a female').

Keep in mind that Woman and woman are different strings. If you intend catch both (or any combination of uppercase and lowercase letters) you should convert user input so that it matches both:

if answer.lower() in {'woman', 'girl', 'lady'}: