TDM 10200: Project 7 - Control Flow / Conditions

Project Objectives

Motivation: Control flow refers to the order in which a set of statements or instructions are executed. Python’s control flow is mainly made up of conditional statements, loops, and function calls.

Context: Conditional statements allow you to execute blocks of code depending on whether a condition is True or False. Loops allow you to repeat the execution of a block of code.

Scope: Python, if, else, loops, iteration

Learning Objectives
  • Understand and use if/else and elif statements

  • Learn some beginner looping practices

  • Practice techniques in both examples sets and full columns

Dataset

  • /anvil/projects/tdm/data/social_media_addiction/social_media.csv

  • /anvil/projects/tdm/data/starwars/characters.csv

If AI is used in any cases, such as for debugging, research, etc., we now require that you submit a link to the entire chat history. For example, if you used ChatGPT, there is an “Share” option in the conversation sidebar. Click on “Create Link” and please add the shareable link as a part of your citation.

The project template in the Examples Book now has a “Link to AI Chat History” section; please have this included in all your projects. If you did not use any AI tools, you may write “None”.

We allow using AI for learning purposes; however, all submitted materials (code, comments, and explanations) must all be your own work and in your own words. No content or ideas should be directly applied or copy-pasted to your projects. Please refer to the-examples-book.com/projects/spring2026/syllabus#guidance-on-generative-ai. Failing to follow these guidelines is considered as academic dishonesty.

Social Media Addiction

Social media is now a huge part of everyday life. This Social Media Dataset aims to show the results of a cross-country survey on the usage patterns, academic impact, and relationship social media has on students and their lives. The sample consists of 705 students aged 16-25, representing 110 countries and 12 different social media platforms.

It is widely recognized that excessive time spent on social media may have negative health effects. The paper of Masri-Zada et al. (2025) concludes that excessive use of social media and digital technology is associated with increased symptoms of anxiety, depression, attention deficits, and addiction-like behaviors in children and adolescents, and that mitigating strategies such as digital literacy, parental monitoring, and targeted interventions are needed to protect youth mental health.*

From the survey, students shared their most used social media platform, how it affected their physical and mental health, and more. Each student rated their addiction score from 1 (low addiction) to 10 (high addiction). The majority of responses were toward the upper end of the scale. When asked about their mental health on a scale of 1 to 10, the results were leaning towards the lower end.

This dataset contains 13 columns. Some of these columns include:

  • Avg_Daily_Usage_Hours: average hours per day on social media,

  • Sleep_Hours_Per_Night: average hours slept nightly,

  • Mental_Health_Score: self-rated mental health score.

*Masri-Zada, Tariq, Suren Martirosyan, Alexander Abdou, Ryan Barbar, Samuel Kades, Hassan Makki, Grant Haley, and Devendra K. Agrawal. "The impact of social media & technology on child and adolescent mental health." Journal of psychiatry and psychiatric disorders 9, no. 2 (2025): 111.

Star Wars

This dataset is on the smaller side - containing just one row for each of less than 100 of the main characters from the Star Wars movie franchise. This data can be useful for finding patterns with what traits there are in characters.

In the Star Wars dataset, there are 13 columns and 96 rows of character descriptions. While some columns such as hair_color and eye_color go to very specific details of each character, and description gives a good summary of how each character is relevant in the movie(s), we will stick to a few specific columns:

  • name: full known name of each character,

  • species: species (when known) of each character,

  • height: measured height in meters.

Five lightsabers fanned on a black background: pink
Figure 1. Five lightsabers in a fan on black background.

Questions

Question 1 (2 points)

There are many colors of lightsabers in the Star Wars universe. The most commonly recognized (specifically in the Skywalker Saga) are blue, green, purple, and red. Each color represents its wielder, often signaling their alignment with the Light or Dark Side, or their rank within the Jedi or Sith orders, such as green lightsabers typically being associated with Jedi Consulars.

This project focuses on control flow methods in Python. An if statement runs a block of code only if a certain condition is True. Something like:

# code will run if condition is True
if condition:

The if and else statements in Python are used to apply conditional logic. A standard structure looks like:

if first_condition:
    # first result
elif second_condition:
    # second result

# add more elif statements as needed

else:
    # default result

For example, let’s check if we need coffee!

need_coffee = True

if need_coffee:
    print("Yes! Grab a coffee first...")
else:
    print("Wow, you must be a superhero! No coffee needed")

Run it one more time with need_coffee = False this time.

The conditionals are commonly used to control the flow of a program based on a condition. Conditionals can be used inside loops, but can also be used within functions.

While not loops themselves, if and else are often used inside loops to evaluate each item in a vector or data structure.

Write an if-elif-else conditional chain that classifies the lightsaber colors into:

  • Jedi Consular - green

  • Jedi Guardian - blue

  • Unique Balance - purple

  • Sith Lord - red

  • Rare Occurrence - any other colors

Your code should determine the classification of a single value stored in a variable called color.

Each if and elif statement should check whether color matches one of the defined color categories. If it does not match any, the final statement (else) should assign it as a 'Rare Occurrence'.

Add a print() statement within each condition level to declare the color of the lightsaber in the result of running the if/elif/else.

Assign color to a color of your choice. This needs to be declared above/before your if/else chain so color will be defined when it is time for it to be classified. Try running the if/elif/else for a few different color values.

So far, we have only checked a single condition at a time. Now, imagine you want to check multiple conditions at once without having to rerun the classifier. For example, predefine the lightsaber colors and their corresponding roles:

lightsaber_roles = {
    "green": "Jedi Consular",
    "blue": "Jedi Guardian",
    "purple": "Unique Balance",
    "red": "Sith Lord"
}

color gets defined using the input() function. In input(), you get to write a message that will appear as the prompt shown when you’re asked to enter a lightsaber color:

color = input("Enter a lightsaber color: ")
print('This means', lightsaber_roles.get(color, "Rare Occurrence"))

"Rare Occurrence" is added to lightsaber_roles.get() to define the output for cases where you chose a lightsaber color that was not preassigned to a role.

Try running

color = input("Enter a lightsaber color: ")
print('This means', lightsaber_roles.get(color, "Rare Occurrence"))

a few times using different inputs. Notice that entering 'blue' gets classified differently from 'Blue' or 'BLUE'.

You may notice that Python is case-sensitive, so blue, Blue, and BLUE are treated differently. To avoid this issue, you can convert the input to lowercase (or uppercase) before performing the lookup:

color = input("Enter a lightsaber color: ").lower()
print('This means', lightsaber_roles.get(color, "Rare Occurrence"))

You may also want to remove accidental spaces:

color = input("Enter a lightsaber color: ").strip().lower()
print('This means', lightsaber_roles.get(color, "Rare Occurrence"))
Deliverables

1.1 Show the result of setting need_coffee to True vs False.
1.2 Output a few results (at least 3) of testing different colors in the if/elif/else.
1.3 Enter a few searches into lightsaber_roles and show the output.

Question 2 (2 points)

Using Pandas, read in the Social Media dataset as myDF from /anvil/projects/tdm/data/social_media_addiction/social_media.csv and show the shape and the first few rows of the data.

It is often the case that for students (ages 18 - 24), there is very little sleep to be had in the day-to-day, but somehow enough time to be on an electronic device - social media alone - for many hours. Looking at the table of both Sleep_Hours_Per_Night and Avg_Daily_Usage_Hours shows that some students are not getting very much sleep (as little as 3.8 hours), while some of the average social media times were as high as a frightening 8.5 hours.

NumPy is a Python library that aims to make it easier to work with numbers - fast, and in large quantities. NumPy is usually read in like import numpy as np.

np.where() operates on an entire pandas column at once, rather than iterating row by row. When this is used, there is no need for Python loops, because this removes the need for row-by-row operations.

An example of using np.where():

import numpy as np

# create 'Age_Status'
# 'Old' values will match those in 'Age' which are >22
# 'Young' values will match those in 'Age' which are <20
# 'Middle' will represent any remaining values
myDF['Age_Status'] = np.where(myDF['Age'] > 22, "Old",
                             np.where(myDF['Age'] < 20, "Young",
                                     "Middle"))

Sometimes it is useful to nest np.where() statements.

Create the column Status to compare the sleep hours to the social media hours of the survey participants. This new column should track values for when:

  • Avg_Daily_Usage_Hours > Sleep_Hours_Per_Night,

  • Avg_Daily_Usage_Hours < Sleep_Hours_Per_Night,

  • Whatever remaining values exist.

For each of these three choices, add some sort of label reflecting the students and their sleep-to-phone ratio, such as Bad Habit, Barely Existing, Doing Fine, Doing Good, Doom Scroll, Fine Habit, Good Habit, Healthy, Lump, Sloth, Thriving, Zombie, and so on.

Print the head of the dataframe to view this new Status column. Use the .value_counts() function to compare the values between the three categories of the column.

Once you have the Status column to categorize the sleep to social media ratio, take a look at some of the row entries of myDF just to compare.

myDF[["Avg_Daily_Usage_Hours", "Sleep_Hours_Per_Night", "Status"]][30:40]
Deliverables

2.1 What was the longest recorded sleep time of the students? The longest social media time?
2.2 Which habit ratio was the most common among the students?
2.3 Display rows 30-39 of the Avg_Daily_Usage_Hours, Sleep_Hours_Per_Night, and Status columns.

Question 3 (2 points)

A while loop runs and repeats until a specified condition is no longer returned as True. The general form is as follows:

while condition:
    # code to be executed until the condition is no longer True
    # This is the loop body
    # These lines must be indented
    # Do any code in here

A while loop will continue running until the condition is False. If there is nothing in the loop that will eventually satisfy this, the loop will go on forever (or until an error breaks it). Infinite loops can cause your environment to freeze or crash, so they should be avoided unless intentionally controlled.

If the lines that follow the initial 'while [condition]:' are not indented, they will not be a part of the loop.

For more information, read about while loops from geeks for geeks page

Say a student’s screen_time is 10 hours. Not even using the Social Media dataset. Just make a simple variable contains the value 10 to represent this:

screen_time = 10

Build a while loop that continues while the screen_time is over 2 hours. While this loop is going, it should print out the student’s screen time. After this, the screen_time variable should decrease by 1. This will print out eight lines, each declaring the student’s screen time, each line one less hour than before.

Use either print(f"{time_variable}") OR print("", [time_variable], "") to combine printing out text and a variable value. It’s up to you. For example:

screen_time = 10

while screen_time > 2:
    print(f"Screen time: {screen_time} hours")
    # OR
    #print("Screen time:", screen_time, "hours")

    screen_time = screen_time - 1

print(f"Final screen time: {screen_time}")

Notice how the while loop continues as long as the condition (screen_time > 2) was TRUE. Once it was FALSE, the loop broke and stopped running. To actually see when screen_time = 2, we need that final print("…​") that is outside of the loop.

Make a second while loop for a variable sleep_time that is equal to 2. This loop should run until sleep_time is no longer less than 10, increasing by 1 each time it finishes. Make sure to print out each value of sleep_time to track its progress.

Finally, build one last while loop that combines screen_time and sleep_time. In this final while loop, print screen_time and sleep_time to track their values. At the end of this loop, screen_time should decrease by .5, and sleep_time should increase by .5. This loop should only run while screen_time is greater than 2.

Don’t forget to reset the values of screen_time and sleep_time between uses. After a loop finishes, these variables will hold their final values rather than their initial ones.

Deliverables

3.1 Display the iterative results from the screen_time loop, and the sleep_time loop.
3.2 Display the results showing the final loops increasing and decreasing the values by 0.5 per iteration, respectively.

Question 4 (2 points)

Read in the Star Wars Character dataset as characters from /anvil/projects/tdm/data/starwars/characters.csv

In pseudocode, the goal of this question is to build a while loop that runs while the character count is less than 20. If the character’s species is Human, mark it as such. Otherwise, mark it in a combined category (non-Human).

To actually go about this, make two variables:

  • i = 0 - go through the rows of the species column. This value is 0 because Python begins counting at row 0 rather than row 1

  • char_count = 0 - count up to 20 characters

While the char_count is less than 20, the loop should continue. At the end of the loop, make sure to increase both i and char_count by 1 each, to move to the next row of the dataset, and increase the running character count, respectively.

In this while loop, we use if and else:

i = 0
char_count = 0

while char_count < 20:
    if characters.loc[i, "species"] == "Human":
        print(f"Character number {i} is a human")
    else:
        print(f"Character number {i} is NOT a human")

    i += 1
    char_count += 1

characters.loc[i, "species"] indicates that the current row being worked with is number i - i.e. if i = 0, the 0th row (first row to occur). If i = 1, the 1st row. And so on.

In this example, both i and char_count increase together, but they represent different concepts: i tracks the dataset row index, while char_count tracks how many characters have been evaluated.

Create the counters human_count and nonhuman_count to track the total number of humans and non-humans, respectively. In place of the print() statements in the while loop, add 1 to the counters.

Add 1 to human_count if the character is human, and add 1 to the nonhuman_count if the character is not human.

Deliverables

4.1 Show the species of the first 20 characters.
4.2 Make counters to find the human and non-human characters.
4.3 How many of the first 20 characters were human? How many non-human? What would happen if there were fewer than 20 rows?

Question 5 (2 points)

Another way to use the while loop is not to put the condition variable at the start. This can be called a "while true" loop, as it loops until the conditions inside of it fail. A while true loop is infinite by default, and will run indefinitely until something makes it stop.

# runs until total_count is greater than 40
# then breaks
# runs indefinitely unless manually broken
while True:
    ....
    ...
    if total_count > 40
        break

# breaks once total_count is no longer less than 40
# runs indefinitely until the condition is false
while total_count < 40:
    ....
    ...

With while True, we must put a condition explicitly inside the body of the loop and use the break statement to exit the loop. Failing to do so will result in an infinite loop.

For an example: define my_list to contain the values 1, 4, 5, 2, 8, 4, 6, 3, 9, 3, 2, 2, 4, 1:

my_list = [1, 4, 5, 2, 8, 4, 6, 3, 9, 3, 2, 2, 4, 1]

Initialize the variables i and total_count as follows (Remember that indexing in Python starts at 0, unlike R, where it starts at 1):

i = 0
total_count = 0

In a while True loop, make my_score equal each i of my_list. total_count should increase by my_score each time. This loop will break if total_count is ever greater than 40, and there will be a celebratory message saying you won. (Do not forget to use i += 1 in the loop.)

To increase i by 1, you can do:
i = i + 1
OR
i += 1

while True:
    my_score = my_list[i]
    print(f"{total_count} + {my_score} = ", end="")
    total_count += my_score
    print(total_count)

    if total_count > 40:
        print("You win!!!!!!!")
        break

    i += 1

The loop does not break until after the point that total_count is greater than 40.

Let us go back to Social Media addiction data that was defined as myDF at the beginning of this project. Using the Mental_Health_Score column from myDF, fill in all ????? in this example:

i = ?????
total_count = ?????

print(f"Starting mental health score is {total_count}\n")

while True:
    student_score = myDF["Mental_Health_Score"][?????]
    print(f"Mental health of student {i} is {student_score}")
    total_count = ????? + student_score
    print(f"Current mental health score is {total_count}\n")

    if total_count >= 100:
        print("All done!!!!")
        break

    i = i + 1
Deliverables

5.1 How do while and while True compare?
5.2 Iterative output of counting up to the final mental health score.

Submitting your Work

Once you have completed the questions, save your Jupyter notebook. You can then download the notebook and submit it to Gradescope.

Items to submit
  • firstname_lastname_project7.ipynb

It is necessary to document your work, with comments about each solution. All of your work needs to be your own work, with citations to any source that you used. Please make sure that your work is your own work, and that any outside sources (people, internet pages, generative AI, etc.) are cited properly in the project template.

You must double check your .ipynb after submitting it in gradescope. A very common mistake is to assume that your .ipynb file has been rendered properly and contains your code, markdown, and code output even though it may not.

Please take the time to double check your work. See here for instructions on how to double check this.

You will not receive full credit if your .ipynb file does not contain all of the information you expect it to, or if it does not render properly in Gradescope. Please ask a TA if you need help with this.