Bird
0
0

You want to define a task that sends emails but only if the email address is not empty. Which of these task definitions correctly applies this condition?

hard📝 state output Q15 of 15
Django - Celery and Background Tasks
You want to define a task that sends emails but only if the email address is not empty. Which of these task definitions correctly applies this condition?
Afrom celery import shared_task @shared_task def send_email(email): if email: # send email code return 'Sent' return 'No email provided'
Bfrom celery import shared_task @shared_task def send_email(email): if email == None: return 'No email provided' # send email code return 'Sent'
Cfrom celery import shared_task @shared_task def send_email(email): if not email: return 'Sent' # send email code return 'No email provided'
Dfrom celery import shared_task def send_email(email): if email: # send email code return 'Sent' return 'No email provided'
Step-by-Step Solution
Solution:
  1. Step 1: Check for correct task decorator and condition

    from celery import shared_task @shared_task def send_email(email): if email: # send email code return 'Sent' return 'No email provided' uses @shared_task and checks if email: which correctly tests for a non-empty email.
  2. Step 2: Verify logic correctness

    from celery import shared_task @shared_task def send_email(email): if email: # send email code return 'Sent' return 'No email provided' returns 'Sent' only if email is truthy (not empty), else returns 'No email provided'. This matches the requirement.
  3. Final Answer:

    Option A correctly defines the task with the condition -> Option A
  4. Quick Check:

    Use if email: to check non-empty string [OK]
Quick Trick: Use if email: to check non-empty strings in tasks [OK]
Common Mistakes:
MISTAKES
  • Forgetting @shared_task decorator
  • Using if not email: incorrectly reversing logic
  • Not indenting task function properly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes