0

Say, I have a function in Python where

if (age<20): young=True if (age>80): old=True 

My question is, if I want to call or print the values which are held as True. I want to write this line: The person wasn't selected for the job because he was old/young etc. One person could also violate many criteria. How would I do that?

0

3 Answers 3

2

Use a dictionary or a custom class. For an isolated call, a dictionary works fine. Here's an example where we initialize values for pre-specified keys as False:

# some arbitrary inputs age, height = 25, 80 # create dictionary with "old" & "tall" keys with values to set to False flags = dict.fromkeys(('old', 'tall'), False) # set dictionary values to True conditionally if age > 20: flags['old'] = True if height > 90: flags['tall'] = True # use list comprehension to iterate dictionary key-value pairs and # return list of keys where value is True true_keys = [k for k, v in flags.items() if v] print(true_keys) ['old'] 
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! But I haven't been taught flags, or dict function. Is there a way I could do this with the if function. Please check out my edited question.
@user585380, No there isn't a good way with just the if function. I strongly suggest you learn about dictionaries. They are fairly elementary objects with a wide range of uses.
Okay. Could you explain the code to me step by step? Or could you just add in the answer itself, the meaning of each line of code. I'm sorry if this is asking for too much but I'm absolutely new to programming.
1

In addition to jpp great answer, you can replace if statements like this:

age, height = 25, 80 flags = dict.fromkeys(('old', 'tall'), False) flags['old'] = age > 20 flags['tall'] = height > 90 true_keys = [k for k, v in flags.items() if v] print(true_keys) 

3 Comments

Thank you! But I haven't been taught flags, or dict function. Is there a way I could do this with the if function. Please check out my edited question.
@user585380, No there isn't a good way with just the if function. I strongly suggest you learn about dictionaries. They are fairly elementary objects with a wide range of uses.
What does k for k, v in flags.items() if v mean?
0

To be sure: you want to print something if old or height is True.

 # This is needed if you want to check if old = false or true old = False height = False 

Your begin is correct.

 if age>20: old=True if height>90: tall=True 

After this, you want to check if old or height is True, so

 if old: print("<Here your output>") if height: print("<Here your output>") 

You can also combine height and old with and, or, not.

However, if you want to print "you're too old", you can also use

 if age>20: print("<text>") if height>90: print("<text>") 

3 Comments

Thanks! What if a person is both? Because for the sake of simplicity, I have just included age and height, but I have the weight criteria as well and all of this is in a range. Therefore, age less than 15 is too young and so on.
@user585380 By using and or or you can check if both or more are set True. Do you mean for x in range(a,b): for height, age and weight? Because if so, you will need to loop thru all three options. However, this will take a while, so it is not the fastest way. If I misunderstood your comment, please leave a comment so I can help you better.
I got it. Thank you so much Jereme Mulder!