Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Nested Conditionals HELP
#1
Hi All!
I am hoping to combine a series of conditions, and I need to find the cleanest possible setup. I have yet to find any that work. My current setup is as follows:
I have my conditions listed as follows:
 
thing_1 = input('What is on thing 1? ')
thing_2 = input('What is on thing 2? ')
thing_3 = input('What is on thing 3? ')
thing_4 = input('What is on thing 4? ')
thing_5 = input('What is on thing 5? ')
thing_6 = input('What is on thing 6? ')
Next I have a series of outputs for those inputs that look like this:
thing_1_A = ["0", "12"]
if thing_1 in thing_1_A:
    print("A")
thing_1_B = ["1", "13"]
if thing_1 in thing_1_B:
    print("B")
thing_2_A = ["1", "13]
if thing_2 in thing_2_A:
    print("A")
ETC...
What I am having trouble with is creating the next output - I need a simple® way to combine conditions
so that when thing_1 = A and thing_2 = B and thing_3 = C, the operation will print ("YOGURT") and still when thing_1 = B and thing_2 = A and thing_3 = C (the same outputs but from different inputs) it will still print ("YOGURT") but when thing_1 = D and thing_2 = E and thing_3 = F, the operation prints ("APPLES")instead. In simple words - when any combination of A B and C are present, I want the same output, but when D is added, I want a different output, or when any other combination is present, another specific output.
This is the setup I am currently working with:
if thing_1 in thing_1_A and thing_2 in thing_2_B and thing_3 in thing_3_C and thing_4 in thing_4_C and thing_5 in thing_5_A and thing_6 in thing_6_A:
    print("This is a condition 1")

I do not see this working out as I would need to write out every possible condition where A and B and C are present, then move on to every possible condition where B and C and D are present, and so fourth. Any ideas? I know a list or array would come of use, I just don't know in which area, if not all.
Thank you for your help!
Reply
#2
I'm not sure I really understand what you are trying to do here, but maybe this would help.

Have a list of types. If thing_1 is in thing_1_A, the first item of types would be 'A'. If thing_1 is in thing_1_B, the first item of types would be 'B'. If thing_2 is in thing_2_A, the second item in types would be 'A'. And so on. Then, this:

if thing_1 in thing_1_A and thing_2 in thing_2_B and thing_3 in thing_3_C and thing_4 in thing_4_C and thing_5 in thing_5_A and thing_6 in thing_6_A:
Translates into:

if types == ['A', 'B', 'C', 'C', 'A', 'A']:
Also, you could do this:

thing_1_map = {('0', '12'): 'A', ('1', '13'): 'B'}
types[0] = thing_1_map[tuple(thing_1)]
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Jul-16-2019, 02:25 PM)ichabod801 Wrote: Hey ichabod801! Thank you for your reply. I did change my code to be secretive in regards to my project, but a similar problem would be as follows:
there are 24 variations of each 6 elements - let them be shoes, glasses, pants, socks, wristwatches, and necklaces. Pretend the 24 variations of each are colors. I need every possible combination of blue, red, and green to be labeled as one output - every possible combination of green, red and blue to be labeled as one output, and so on. Does this help? I am making sense of your response now.
Thank you again!
Absolum
Hey ichabod801! Thank you for your reply. I did change my code to be secretive in regards to my project, but a similar problem would be as follows:
there are 24 variations of each 6 elements - let them be shoes, glasses, pants, socks, wristwatches, and necklaces. Pretend the 24 variations of each are colors. I need every possible combination of blue, red, and green to be labeled as one output - every possible combination of green, red and blue to be labeled as one output, and so on. Does this help? I am making sense of your response now.
Thank you again!
Absolum
Reply
#4
This is getting awfully vague and obtuse, which makes it hard to help. Maybe you could make a list comprehension of to generate all the variants of the pattern you want, and then use the in operator to see if the input is one of the variants.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Thank you for your help! I found my way around the problem, and whilst using your advice! Sorry for coming off as vague or obtuse, I do not yet have the coding vocabulary to communicate properly in a forum like this. :) If you have any resources to recommend, please do so! I am learning as I go for now. Thanks again!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Doubt about conditionals in Python. Carmazum 6 1,536 Apr-01-2023, 12:01 AM
Last Post: Carmazum
  conditionals based on data frame mbrown009 1 873 Aug-12-2022, 08:18 AM
Last Post: Larz60+
  Efficiency with regard to nested conditionals or and statements Mark17 13 3,080 May-06-2022, 05:16 PM
Last Post: Mark17
  Nested conditionals vs conditionals connected by operators dboxall123 8 2,983 Feb-18-2022, 09:34 PM
Last Post: dboxall123
  Nested Conditionals shen123 3 2,584 Jul-28-2021, 08:24 AM
Last Post: Yoriz
  Invalid syntax using conditionals if - else jperezqu 1 2,295 Jan-13-2021, 07:32 PM
Last Post: bowlofred
  conditionals with boolean logic?? ridgerunnersjw 3 1,958 Sep-26-2020, 02:13 PM
Last Post: deanhystad
  two conditionals with intermediate code Skaperen 5 2,713 Jul-12-2020, 07:18 PM
Last Post: Skaperen
  Conditionals, while loops, continue, break (PyBite 102) Drone4four 2 2,923 Jun-04-2020, 12:08 PM
Last Post: Drone4four
  Branching and Conditionals/If statements MSL 1 1,889 Jan-26-2019, 11:52 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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