Building My Toolkit: Python Pt. 3 (User Input and While Loops)


Building My Toolkit is a series in which I document my learning of various tech and data tools. Ultimately, my goal is to incorporate these skillsets in my role as an institutional research practitioner. Each week, I discuss the progress I’ve made with a particular tool, currently the Python programming language.

Up until now, the coding examples have been relatively static — meaning that I hardwired the code snippets to work exactly as I want to, and there is no input on the part of the user to direct the flow of a program. Naturally, you can imagine that this is not very realistic, as many roles in higher education rely on technology and the information that we input into our computers. Fortunately the Python syntax to prompt for user input is quite simple:


name = input("What is your name? ")

In this example, I use the input function to prompt the user for their name by telling the user what we want them to do. There are also ways to validate the data, such as checking to make sure that no integers are entered into the name field or we might use the title function to ensure that even if the user were to enter ‘JOE’, ‘jOe’, or ‘joe’, as names, they would each get passed into the program as ‘Joe’. Incorporating user input into future projects will allow me to create dynamic and user-driven programs, such as a simple text-based role-playing game. On a very fundamental level, the same sort of logic is at work when users enter their credentials into an e-mail client or other software.

Another handy feature that I learned about this week was the use of while loops. Essentially, while loops do what the name implies: While a condition X or Y (or both!) is unmet, a block of programming logic will continue to execute. For example:


current_number = 1

while current_number < 5:
    print(current_number)
    current_number += 1

In this example, I tell the program to print the current number to the terminal, which I initialize with a value of 1. As long as the number is less than 5, the program will add 1 to the current value and print the new number until the condition (number < 5) is satisfied. And so the result of this program would simply be:

1
2
3
4

This sort of logic is quite common in a variety of cases. The find and replace feature in Microsoft Office is a great example. As long as the program detects another iteration of the user’s query, the program will proceed to highlight and replace that iteration. Again, the logic is oversimplified here, but I challenge you to consider other ways in which the while loop logic drives other applications that you may work with.

So far, we’ve talked about all of the neat things we can do through programming logic, from continuing to loop through certain blocks of code to executing certain code based on a given value. Next time we’ll take a look at functions, which are handy ways to bundle various bits of code together in a handy way that we can call upon as needed in our program.

Building My Toolkit: Python Pt. 2 (If Statements and Dictionaries)

Building My Toolkit is a series in which I explore my learning of various tech and data tools. Ultimately, my goal is to incorporate these skillsets in my role as a student affairs practitioner. Each week, I discuss the progress I’ve made with a particular tool, currently the Python programming language.

The last week or so has been a really exciting time in my fledgling programming career. The programming problems that I’ve been working on out of the Python Crash Course have become noticeably more complex, offering many possibilities for how to tackle a given program. I’m particularly excited that I got to tackle if statements this week, as they are a great supplement to for loops. In addition, they let me know that tackling while loops isn’t too far off on the horizon.

If statements are pretty straightforward — they instruct a computer program to execute a command if a certain condition is met. The below code snipped illustrates a rather simple usage of an if statement.


for student in student_list:

if student_age 

    print("This student is underage!")

else:

    print(student_name)

The above snippet would loop through a list of students and print the name of each statement. The use of the if statement here directs the program to let me know if a student is underage. This is a simplistic snapshot of some of the backend logic with Maxient, Advocate, and other conduct management software programs.

Another example of if statements working behind the scenes is the condition feature in Excel. As higher education administrators, we practically live in a world of spreadsheet at times — or so it feels. A simple if statement helps on the backend of Excel to check whether certain cases are true.

I could also use pseudocode — a high-level outline of a computer program or algorithm to show what the programming logic for a simple conditional formatting might look like.

# Pseudocode that outlines the logic for a basic conditional formatting
# operation in Excel Loop through each cell selected by the user

Loop through each cell selected by the user
For each cell:
if the cell == null # Meaning that the cell does not contain any value
Change the cell shade to light red
else: Change the cell shading to light green

In my studies from the past week, I also explored dictionary data structures, which are used to connect related pieces of information. For example, I could create a dictionary of student development theorists and connect the theorists’ name to their respective theory as follows:


theorists = {

'schlossberg': 'transition theory',

'kohlberg': 'moral development',

'chickering': 'seven vectors',

'sanford': 'challenge and support',

}

At their core, dictionaries are composed of key-value pairs. Essentially a key-value pair is jargon for two pieces of data that are connected. The above example is a dictionary called ‘theorists’ and contains a total of four key-value pairs. Each pair of data is conjoined with a colon, with single quotes used to set off each piece of data.  Dictionaries are nifty in that their functionality can be scaled up in a number of ways. For instance, we could use a list to associate a number of theoretical ideas with a particular theorist. Let’s revisit Schlossberg’s transition theory. Instead of having one value associated with the key ‘schlossberg’, we could associate the four S’s of her theory with the same key.


'schlossberg': ['situation', 'self', 'support', 'strategies']

I could go on down the rabbit hole of possibilities with dictionaries, but I think I’ll stop there. It’s exciting to be getting to a part of my programming journey where there are more nuanced decisions to be made about how I approach a given task. I think my next post in this series will be a real treat! I hope you’ll join me as I look at some more programming logic, building some nifty programming blocks, and how to take  user input right from the keyboard.

Thanks for reading,

Joe

Are you interested in giving programming a try for yourself? Check out this link for a brief Ruby tutorial. Ruby is a high-level programming language that is similar to Python. Let me know if you got the chance to check it out!

Let’s connect on Twitter @joe_diodato.