Python Forum
searching within specified variable list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
searching within specified variable list
#1
im trying to create a system which asks for a specific shirt size and colour. white is avaliable in l and m and blue is avaliable in m and s.
im struggling to get the code for searching if the size needed is avaliable. I know that coloursize is undefined, but im trying to search in the 'bluesize' or 'whitesize' list of specified variables depending on what colour is inputted by the user! im still very new to coding and trying to teach myself so any help and tips would be appreciated! thank you.

whitesize= "L" or "M"
bluesize= "M" or "S"
colour= "white" or "blue"
size_needed= input("what size?: ")
colour_needed= input("What colour: ")

for colour_needed in colour:
    if size_needed in coloursize:
        print("true")
    else:
        print("false")
Reply
#2
whitesize and blussize are not searchable.
>>> whitesize= "L" or "M"
>>> whitesize
'L'
>>> bluesize= "M" or "S"
>>> bluesize
'M'
>>>
you need something like a dictionary:
>>> choices = {'white': ['L', 'M'], 'blue': ['M', 'S']}
>>> color_needed = 'white'
>>> size_needed = 'S'
>>> print(size_needed in choices[color_needed])
False
>>> color_needed = 'white'
>>> size_needed = 'M'
>>> print(size_needed in choices[color_needed])
True
>>>
Reply
#3
Hi, thanks for your reply. I want to ask the user for input on what colour and size required, but this response has given me a head start! thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Split string using variable found in a list japo85 2 1,296 Jul-11-2022, 08:52 AM
Last Post: japo85
  Searching in the list quest 4 1,524 Mar-02-2022, 10:30 AM
Last Post: quest
  An IF statement with a List variable dedesssse 3 8,218 Jul-08-2021, 05:58 PM
Last Post: perfringo
  Create variable and list dynamically quest_ 12 4,398 Jan-26-2021, 07:14 PM
Last Post: quest_
Question Matching variable to a list index Gilush 17 5,857 Nov-30-2020, 01:06 AM
Last Post: Larz60+
  Print variable values from a list of variables xnightwingx 3 2,626 Sep-01-2020, 02:56 PM
Last Post: deanhystad
  Multiplication between a list and a variable doug2019 2 2,162 Oct-08-2019, 04:10 AM
Last Post: doug2019
  Sub: Python-3: Better Avoid Naming A Variable As list ? adt 9 4,008 Aug-29-2019, 08:15 AM
Last Post: adt
  Score each word from list variable pythonias2019 6 3,348 Jun-13-2019, 05:44 PM
Last Post: gontajones
  How to store the value from variable into list & run a statement then put in variable searching1 1 2,449 May-29-2019, 06:36 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

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