Skip to content

This project will integrate input, conditionals, loops, and functions to create a program that runs an interactive flowchart for the user.

Prep: Find or draw a flowchart

You want a flowchart that has around 4-8 questions/forks/decision points and 5-10 endpoints. If your flowchart is too simple, I will ask you to make it more interesting. If your flowchart is too complicated, I will ask you to start with just a part of it.

Once you’ve chosen a flowchart, please email it to me (attached image file, link, whatever works) so that I can confirm it’s a good choice for this project!

Some tips before you get started

  1. Run your code often to test it! It sucks to write ten lines of code, then get an error message and not know what’s causing it.
  2. Pay attention to your indentations. My examples here have 4 spaces for each tab, because that’s how I prefer it. Repl.it defaults to 2 spaces. It doesn’t matter what you choose as long as you stay consistent!
  3. Use comments to annotate your code. If you have an error or bug that you need help with, I will be able to read your code much more easily if you have comments saying what each function is supposed to do.
  4. Related to #3, please use blank lines between chunks of code to make your program more readable!
  5. When you get an error message, read the last line to see what kind of error it is. If you can’t figure it out, email me!

Writing your program

Intro

Start by writing an intro() function that prints a nice title and explains to the user what your flowchart will do. (Use triple-quoted strings to control line breaks.)

For example:

# intro message to user
def intro():
    print('''
WHAT KIND OF BIRB?

This flowchart will help you
identify your backyard flappers,
cheepers, and squawkers.''')

(Remember that in triple-quoted strings, any indents will be printed exactly as you type them. These are the only time you will have un-indented lines within a function definition.)

Don’t forget to call your function at the bottom of your program! Run your code to make sure it’s working.

First question

Below your intro(), define a choose() function. For now, just put an empty return statement in it—this is just a placeholder for a function we’ll come back and define later.

def choose():
    return

Next, write a function that offers the user their first decision. Give this function a descriptive name—rather than firstChoice() or decision1, call it something that describes what the user is deciding.

This function will print the question, then a numbered list of options. (You can offer as few as two options, or as many as you want!)

# first choice, what color is birb
def birbColor():
    print('''
What color is your feather-
friend? (It probably has lots
of colors. Just choose the most
obvious one.)''')
    print('1) Yellow')
    print('2) Red')
    print('3) Blue')
    print('4) Tan/Brownish')
    print('5) Black/Grey')
    print('6) None of these')

Call this function at the bottom of your program. Run your code: it should print your intro and the list of options (but you won’t be able to choose one yet).

choose() function

Now we will return to the choose() function. This function will use a loop to ask the user to make a choice and check that it’s valid, and then return the option number.

Depending on which choice is being made, the user will have a different number of options. We will give the choose() function a parameter so that it can take the number of choices as input.

# gets choice from user and makes sure it's valid
def choose(numChoices):

We’ll use a while loop to get a valid choice. This requires defining a sentinel variable to initiate the loop—I’ll call it choice. We’ll set it equal to zero, because that isn’t a valid option. The while loop will check if the value of choice is too low or too high. After the loop completes (a valid choice), we return the value of the option selected by the user.

    choice = 0
    while choice < 1 or choice > numChoices:
        choice = input("Type the number and hit Enter.")
        choice = int(choice)
    return choice

Now we’ll go back into the function that asks the user the question and call the choose() function there, entering the number of choices in the parentheses (in my case, 6).

    return choose(6)

A quick step back to look at what these functions are doing:

  1. main.py (the program) calls birbColor()
  2. birbColor() calls choose(6)
  3. choose(6) returns a value between 1 and 6 to birbColor()
  4. birbColor() returns that value to main.py

But right now, the program doesn’t have any way to “catch” the value being returned from birbColor() (or whatever your function is called). We need to create a variable to catch and save that value. At the bottom of your program, where you call the decision function, add a variable assignment to the beginning of the line:

choice = birbColor()

Run this code. It should print your intro, ask the user to make the first choice (and keep asking until they enter a valid number), then end.

A fork in the road

Now that you’ve collected the user’s choice, it’s time to make your program respond differently depending on what the response was. At the bottom of your program, add a conditional:

if choice == 1:
    yellowBirb()
elif choice == 2:
    redBirb()
[etc.]
elif choice == 5:
    blackBirb()
else:
    otherColorBirb()

Now, you’ll need to write each of the functions called by the conditional. Beneath the definition of your first decision function, add definitions for the new functions. These should provide the next step in your flowchart based on the responses. For example, if my user sees a yellow bird, I’ll ask them how big the bird is:

def yellowBirb():
    print('Yellow birb! How escite!')
    print('How big is birb?')
    print('1) smol like sparrow or golfball')
    print('2) bigger than that')
    return choose(2)

Once you’ve defined each of these functions, test run your code! Try selecting each of the possible options to make sure everything’s working. The new functions you just wrote should run and offer you another choice.

Nested conditionals

In order to handle the fork suggested by my yellowBirb() function, I’ll need to add a conditional in my code to handle the different options made. However, because this choice only happens if the bird is yellow, I can’t add this new conditional at the end of my program. I have to put it inside the existing conditional, under the if choice == 1 condition:

if choice == 1:
    choice = yellowBirb()
    if choice == 1:
        goldfinch()
    else:
        idk()

Notice I am recycling the variable choice, assigning it a new value (the output of yellowBirb()) on line 2 and then using it for the following conditional. This helps make my code more efficient by avoiding the creation of new variables and disuse of old variables.

It’s also okay to use a different variable for this second choice, if you prefer. For example, instead of choice I could have named the first variable color to note that it’s holding the user’s choice of color. Then the next variable in my code above could be size. Again, this is less efficient, but it can make it easier to keep track of what’s going on in your program.

Go ahead and fill out your conditional structure with these nested conditionals, making sure to keep a close eye on your indentations. Then, you’ll write the functions called by these conditionals.

The End

When you reach an endpoint in your flowchart, create a function for that endpoint the same as you would for a next-decision-point. In that function, tell the user what their outcome is and let them know they’ve reached the end. You don’t have to do anything special in your code to make the program end: just leave out the choose() function and don’t use a return statement.

In your tree of nested conditionals that runs your flowchart, an endpoint will look like a function that is simply called on a line, rather than assigned to a variable (e.g., theEnd() rather than choice = theEnd()).

Each time you write an endpoint, make sure that you reach that endpoint by following the correct path in your flowchart.

When you’ve finished your flowchart, run it several times to make sure it’s working correctly. Make sure that each decision point does what you’d expect, and make sure you can reach each endpoint by following the expected path. Once you’ve checked it over for bugs, please send me your repl link so I can look over it! And if you’d like to show it to the class, let me know and I’ll give you a chance to share it during the next class.

Example flowchart program

You can check out the bird ID flowchart program to get a sense of how it all fits together.

Skip to toolbar