Nested Conditionals HELP - 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: Nested Conditionals HELP (/thread-19829.html) |
Nested Conditionals HELP - absolum - Jul-16-2019 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! RE: Nested Conditionals HELP - ichabod801 - Jul-16-2019 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)] RE: Nested Conditionals HELP - absolum - Jul-16-2019 (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: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 RE: Nested Conditionals HELP - ichabod801 - Jul-16-2019 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. RE: Nested Conditionals HELP - absolum - Jul-17-2019 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! |