pythonboolean-operations

Boolean operators: Branching using Boolean variables ( python)


I'm working on a homework question and I'm having a terrible time getting it to return the final statement correctly.

Instruction:

Write an expression that prints 'You must be rich!' if the variables young and famous are both True.

Code (I'm only able to update the if statement):

young = True
famous = False
if (young == 'True') and (famous == 'True'):
    print('You must be rich!')
else:
    print('There is always the lottery...')

My initial thoughts are the combination in the code above, but I'm desperate and I've tried all the combinations below as well:

if (young != 'True') and (famous == 'True'): 
if (young == 'True') and (famous != 'True'): 
if (young == 'True') and (famous != 'False'): 
if (young == 'True') or (famous == 'True'): 
if (young == 'True') or (famous != 'True'): 
if (young == 'True') or (famous == 'True'): 
if (young == 'True') or (famous != 'False'):

The results:

Testing with young and famous as both False
Your output: There is always the lottery...

Testing with young as True and famous as False
Your output: There is always the lottery...

Testing with young as False and famous as True
Your output: There is always the lottery...

āœ– Testing with young and famous as both True
Expected output: You must be rich!
Your output: There is always the lottery...


Solution

  • Apparently you are confused between boolean variables and strings

    young=True #boolean variable
    

    and

    young='True' #string
    

    Here is the corrected code

    young = True
    famous = False
    if young and famous:
        print('You must be rich!')
    else:
        print('There is always the lottery...')
    

    I would recommend you to go through your lessons on strings and boolean variables before using this,Good luck