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...

3

1 Answer

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

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest