user6762618
user6762618

Reputation:

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

Upvotes: -1

Views: 11764

Answers (2)

AlexMayaral
AlexMayaral

Reputation: 1

Do not use '' because input already turns the input into strings, AKA 'True'. If you use and and both are true it will be true.

young = (input() == 'True')
famous = (input() == 'True')

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

Upvotes: 0

Amal Vincent
Amal Vincent

Reputation: 154

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

Upvotes: 2

Related Questions