Q1. Explain and demonstrate the use of Conditional statements to SmartFit team. Explain how if, elif, and else statements work in Python: Explain the purpose of each statement. Explain how Python decides which block of code to execute. Write a Python program for SmartFit that asks the user to enter their age. If the age is below 18, print: "You are eligible for the Teen Fitness Program." If the age is between 18 and 40, print: "You are eligible for the Regular Fitness Program." Otherwise, print: "You are eligible for the Senior Wellness Program." Q2. Demonstrate the use of logical operators. Construct Boolean expressions by extending your program to ask the user if they have any medical conditions (yes or no). If the user is 40 or older AND has a medical condition, print: "Medical clearance required before joining." If the user is under 40 OR has no medical condition, print: "You can proceed with registration." If the user enters an invalid response, print: "Invalid input. Please enter 'yes' or 'no'." Q3. Demonstrate the use of Nested Conditional statements and Nested logical operators. Implement Nested Conditional statements by extending your program. SmartFit offers two types of membership: Basic and Premium. Ask the user to choose a membership type. If the user selects Basic, ask if they want personal training (yes/no): If yes, print: "Basic plan with personal training: 45 per month." If no, print: "Basic plan: 30 per month." If the user selects Premium, print: "Premium plan: $60 per month." Implement Nested Conditionals with logical operators to your existing program. If the user has Premium membership and their age is under 30, print: "You qualify for a youth discount! 10% off your plan." If the user has Basic membership and does not want personal training, print: "Consider upgrading to Premium for more benefits!" If the user has a medical condition and chooses Premium, print: "We recommend a free consultation before starting."

Psychology

ScanSolve AI Answer

Q1. Conditional Statements: if, elif, and else

Conditional statements allow a program to make decisions and execute different blocks of code based on whether certain conditions are true or false.

  • if statement: This is the primary conditional statement. The code block indented below if executes only if its condition evaluates to True.
  • elif statement: Short for "else if," elif is used to check an additional condition if the preceding if (or elif) condition(s) were False. You can have multiple elif statements.
  • else statement: This block of code executes if all preceding if and elif conditions in the structure evaluate to False. It acts as a default case.

Python decides which block of code to execute by evaluating the conditions sequentially from top to bottom. The first condition that evaluates to True will have its corresponding code block executed, and the rest of the if-elif-else structure is then skipped. If no if or elif condition is True, the else block (if present) is executed.

Q2. Logical Operators: and, or, not

Logical operators are used to combine or modify Boolean expressions, allowing for more complex conditions.

  • and operator: Returns True if both conditions it connects are True. Otherwise, it returns False.
  • or operator: Returns True if at least one of the conditions it connects is True. It returns False only if both conditions are False.
  • not operator: Reverses the Boolean value of a condition. If a condition is True, not makes it False, and vice-versa.

Q3. Nested Conditional Statements and Nested Logical Operators

  • Nested Conditional Statements: These occur when one if, elif, or else statement is placed inside another. This allows for more granular decision-making, where an outer condition must be met before an inner condition is even considered.
  • Nested Logical Operators: This refers to using logical operators within the conditions of nested if/elif/else statements, or combining multiple logical operators in a single complex condition.

Here is the Python program demonstrating all the requirements:

# Q1: Ask for age and determine program eligibility
try:
    age = int(input("Please enter your age: "))

    if age < 18:
        print("You are eligible for the Teen Fitness Program.")
        program_type = "Teen"
    elif 18 <= age <= 40:
        print("You are eligible for the Regular Fitness Program.")
        program_type = "Regular"
    else:
        print("You are eligible for the Senior Wellness Program.")
        program_type = "Senior"

    # Q2: Ask about medical conditions and use logical operators
    medical_condition_input = input("Do you have any medical conditions? (yes/no): ").lower()
    medical_condition = None # Initialize to handle invalid input

    if medical_condition_input == 'yes':
        medical_condition = True
    elif medical_condition_input == 'no':
        medical_condition = False
    else:
        print("Invalid input. Please enter 'yes' or 'no'.")
        # Exit or handle invalid input more robustly in a real application
        medical_condition = None # Ensure it's clearly invalid

    if medical_condition is not None: # Only proceed if medical_condition is valid
        if age >= 40 and medical_condition:
            print("Medical clearance required before joining.")
        elif age < 40 or not medical_condition: # 'not medical_condition' is equivalent to medical_condition == False
            print("You can proceed with registration.")

    # Q3: Implement Nested Conditional statements and Nested logical operators
    membership_type = input("Choose a membership type (Basic/Premium): ").lower()

    if membership_type == 'basic':
        personal_training_input = input("Do you want personal training? (yes/no): ").lower()
        personal_training = None

        if personal_training_input == 'yes':
            personal_training = True
            print("Basic plan with personal training: $45 per month.")
        elif personal_training_input == 'no':
            personal_training = False
            print("Basic plan: $30 per month.")
        else:
            print("Invalid input for personal training. Please enter 'yes' or 'no'.")
            personal_training = None # Mark as invalid

    elif membership_type == 'premium':
        print("Premium plan: $60 per month.")
        personal_training = False # Not applicable for premium in this logic, or assume no personal training
    else:
        print("Invalid membership type. Please choose 'Basic' or 'Premium'.")
        membership_type = None # Mark as invalid

    # Nested Conditionals with logical operators
    if membership_type == 'premium' and age < 30:
        print("You qualify for a youth discount! 10% off your plan.")

    if membership_type == 'basic' and personal_training is not None and not personal_training:
        print("Consider upgrading to Premium for more benefits!")

    if medical_condition and membership_type == 'premium':
        print("We recommend a free consultation before starting.")

except ValueError:
    print("Invalid age input. Please enter a number.")

Example Output:

Please enter your age: 25
You are eligible for the Regular Fitness Program.
Do you have any medical conditions? (yes/no): no
You can proceed with registration.
Choose a membership type (Basic/Premium): basic
Do you want personal training? (yes/no): no
Basic plan: $30 per month.
Consider upgrading to Premium for more benefits!
Please enter your age: 45
You are eligible for the Senior Wellness Program.
Do you have any medical conditions? (yes/no): yes
Medical clearance required before joining.
Choose a membership type (Basic/Premium): premium
Premium plan: $60 per month.
We recommend a free consultation before starting.
Please enter your age: 22
You are eligible for the Regular Fitness Program.
Do you have any medical conditions? (yes/no): no
You can proceed with registration.
Choose a membership type (Basic/Premium): premium
Premium plan: $60 per month.
You qualify for a youth discount! 10% off your plan.
Was this helpful?

Still stuck on this one?

Ask a follow-up, and the answer starts from this question.

Got a different question?Ask your own question