Python Forum
An IF statement with a List variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
An IF statement with a List variable
#1
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!")
Reply
#2
Try this
answer = input('Enter your gender: ').strip()
if answer in {'woman', 'girl', 'lady'}:
    print('You are a woman')
Reply
#3
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'.
jefsummers and ndc85430 like this post
Reply
#4
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'}:
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List Creation and Position of Continue Statement In Regular Expression Code new_coder_231013 3 585 Jun-15-2022, 12:00 PM
Last Post: new_coder_231013
  Change a list to integer so I can use IF statement buckssg 3 1,329 Sep-21-2021, 02:58 AM
Last Post: bowlofred
  How to invoke a function with return statement in list comprehension? maiya 4 1,421 Jul-17-2021, 04:30 PM
Last Post: maiya
  Create variable and list dynamically quest_ 12 2,572 Jan-26-2021, 07:14 PM
Last Post: quest_
Question Matching variable to a list index Gilush 17 3,443 Nov-30-2020, 01:06 AM
Last Post: Larz60+
  define a variable using an if statement Margaridalopes 2 1,395 Oct-24-2020, 05:47 PM
Last Post: jefsummers
  using element on a list as condition statement glennford49 11 3,100 May-21-2020, 12:53 PM
Last Post: hussainmujtaba
  Help: list comprehension for loop with double if statement mart79 3 1,509 May-04-2020, 06:34 AM
Last Post: buran
  Taking brackets out of list in print statement pythonprogrammer 3 1,639 Apr-13-2020, 12:25 PM
Last Post: perfringo
  printing a list contents without brackets in a print statement paracelx 1 1,477 Feb-15-2020, 02:15 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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