Skip to article frontmatterSkip to article content

Exercise 1 - Data Types, Variables, Operators, and Conditionals

  • ๐Ÿ† 20 points available
  • โœ๏ธ Last updated on 9/10/2025

โ–ถ๏ธ First, run the code cell below to import unittest, the module used for the ๐Ÿงญ Check Your Work boxes and the autograder.

# DO NOT MODIFY THE CODE IN THIS CELL
import unittest
tc = unittest.TestCase()

๐ŸŽฏ Challenge 1: Netflixโ€™s Debt-to-Equity Ratio ๐Ÿ’ตยถ

๐Ÿ‘‡ Taskยถ

The debt-to-equity ratio shows the proportion of debt and equity a company uses to finance its assets.
It is calculated by dividing total debt by total equity.

Your goal is to calculate Netflixโ€™s debt_to_equity_ratio using the variables netflix_total_debt and netflix_total_equity. Store the result to a new variable named debt_to_equity_ratio.

๐Ÿ’ก Hintยถ

For example, 25 / 10 evaluates to 2.5.

netflix_total_debt =  15582804
netflix_total_equity =  24743567 

# YOUR CODE BEGINS

# YOUR CODE ENDS

print(f"Netflix's debt-to-equity ratio is {round(debt_to_equity_ratio, 2)}")

๐Ÿงญ Check Your Workยถ

Run the code cell below to test your solution.

  • โœ”๏ธ If the code cell runs without errors, youโ€™re good to move on.
  • โŒ If the code cell produces an error, review your code and fix any mistakes.
_test_case = 'calculate-debt-to-equity-ratio'
_points = 4

tc.assertAlmostEqual(debt_to_equity_ratio, 0.6297719322359626)

๐ŸŽฏ Challenge 2: Movie vs. TV ๐ŸŽฌ๐Ÿ“บยถ

๐Ÿ‘‡ Taskยถ

  • p_movie contains the probability (0โ€“1) that a person prefers movies.
  • p_tv contains the probability (0-1) that a person prefers TV shows.
  • If p_movie is greater than p_tv, set result to "Movie".
  • Otherwise, set result to "TV".
p_movie = 0.47
p_tv = 0.53
result = None

# YOUR CODE BEGINS

# YOUR CODE ENDS

print(result)

๐Ÿงญ Check Your Workยถ

Run the code cell below to test your solution.

  • โœ”๏ธ If the code cell runs without errors, youโ€™re good to move on.
  • โŒ If the code cell produces an error, review your code and fix any mistakes.
_test_case = 'movie-vs-tv'
_points = 4

tc.assertEqual(result, "TV")

๐ŸŽฏ Challenge 3: Shipping Fee ๐Ÿ›’ยถ

๐Ÿ‘‡ Taskยถ

The amount variable contains the total order amount in dollars.

In this exercise, amount will be randomly generated each time you run the code (between 1 and 150).

Your task is to set a variable named shipping_fee to one of the following values based on amount:

  • "Free Shipping" if amount >= 100
  • "Discounted Shipping" if 50 <= amount < 100
  • "Standard Shipping" if amount < 50

๐Ÿ“˜ Exampleยถ

If amount = 75, then:

  • shipping_fee is "Discounted Shipping".

๐Ÿ“ Rulesยถ

  • Use an if / elif / else structure to complete this task.
  • Do not hardcode the result; your code should work for any randomized amount value.
import random
amount = random.randint(1, 150)    # purchase amount between $1 and $150

# YOUR CODE BEGINS

# YOUR CODE ENDS

print(f"Order amount: {amount}")
print(f"Shipping fee: {shipping_fee}")

๐Ÿงญ Check Your Workยถ

Run the code cell below to test your solution.

  • โœ”๏ธ If the code cell runs without errors, youโ€™re good to move on.
  • โŒ If the code cell produces an error, review your code and fix any mistakes.
_test_case = 'shipping-fee'
_points = 6

shipping_fee_SOL = (
    "Free Shipping" if amount >= 100
    else "Discounted Shipping" if amount >= 50
    else "Standard Shipping"
)

tc.assertEqual(shipping_fee, shipping_fee_SOL)

๐ŸŽฏ Challenge 4: Scholarship Eligibility ๐ŸŽ“ยถ

๐Ÿ‘‡ Taskยถ

The following variables are given:

  • gpa โ†’ a studentโ€™s grade point average (0.0โ€“4.0)
  • credits โ†’ the total number of credits the student has earned
  • volunteer_hours โ†’ the number of volunteer hours completed

Your task is to determine eligibility for two scholarships:

  1. Merit Scholarship

    • Eligible if gpa >= 3.5 and credits >= 30
  2. Community Scholarship

    • Eligible if gpa >= 3.0 or volunteer_hours >= 100

Set two Boolean variables:

  • merit_eligible โ†’ True if the student qualifies for the Merit Scholarship, otherwise False
  • community_eligible โ†’ True if the student qualifies for the Community Scholarship, otherwise False

๐Ÿ“˜ Exampleยถ

gpa = 3.6
credits = 28
volunteer_hours = 120
  • merit_eligible is False (credits < 30)
  • community_eligible is True (volunteer hours >= 100)

๐Ÿ“ Rulesยถ

  • Use logical operators (and, or) to complete this task.
  • Do not hardcode the results; your code should work for any values of gpa, credits, and volunteer_hours.
import random
gpa = round(random.uniform(0.0, 4.0), 2)    # GPA between 0.00 and 4.00
credits = random.randint(0, 60)             # credits between 0 and 60
volunteer_hours = random.randint(0, 200)    # volunteer hours between 0 and 200

# YOUR CODE BEGINS

# YOUR CODE ENDS

print(f"GPA: {gpa}")
print(f"Credits: {credits}")
print(f"Volunteer Hours: {volunteer_hours}")
print(f"Merit Scholarship Eligible? {merit_eligible}")
print(f"Community Scholarship Eligible? {community_eligible}")

๐Ÿงญ Check Your Workยถ

Run the code cell below to test your solution.

  • โœ”๏ธ If the code cell runs without errors, youโ€™re good to move on.
  • โŒ If the code cell produces an error, review your code and fix any mistakes.
_test_case = "scholarship-eligibility"
_points = 6

merit_eligible_SOL = True and (not False) and gpa >= 3.5 and credits >= 30
community_eligible_SOL = False or (not True) or gpa >= 3.0 or volunteer_hours >= 100

tc.assertEqual(merit_eligible, merit_eligible_SOL)
tc.assertEqual(community_eligible, community_eligible_SOL)