[Python] Boolean operators: Branching using Boolean variables ( python)

Discussão em 'Python' iniciado por Stack, Fevereiro 10, 2025.

  1. Stack

    Stack Membro Participativo

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

    Continue reading...

Compartilhe esta Página