Brushing Up My (very) Rusty Python Skill by Coding a Simple Program

Going through the full process of software development on a tiny scale can help you regain your coding skillset

Peter Kane
Jan 15 · 14 min read

I’m sure I’m not exaggerating saying that I’ve always been fascinated with coding. I’ve crossed paths with many programming languages, including Pascal, Microsoft Visual Basic, Java, C++, and, the latest, Python.

I’ve always been a partial part-time coder (less than a full part-time) until this point in life. I’ve been coding on and off, here and there, collecting bits and pieces of computer programming wonders. I’ve been coding less and less, not because I don’t like it anymore but because my career doesn’t really call for this ability.

But the fact that our present lives are wrapped-up by codes (algorithms) makes this a perfect time to revisit coding. And today’s star language is, you guessed it, Python.

As someone who also thinks there’s nothing wrong going with the current, I’m also fascinated by the Data Science hype. OK. To do it justice, Data Science is no hype. It’s the future of humanity. And Python is like a hero who saves us from drowning in the bottomless ocean of data.

As a programming language, Python can help us harness data and extract meaning from it. Python is one of the essential tools in the Data Science toolbox. Not only can we use Python to pull large amounts of data from different sources, but we can also use Python to clean and extract meaningful information out of those numbers and texts in the form of visually-pleasing and easy-to-understand charts. That’s why people with Python coding skills will be more needed in the future job market.

My relationship with Python is no different than with any other programming language. I’ve been coding with Python (on and off) since 2013, the time I was also trying to finish my Ph.D. in Chemistry. Life went on, and I coded less. And naturally, my Python coding skills have gotten very rusty when I write this article.

After a period of zero coding, I decided to brush up on my skill again with Python, and I would like to share this experience with you.

There’s an easy, straightforward program one can code with Python from my recently revisited online course. The mechanism is so simple: the program takes some information from the user and displays it in subsequent lines. Yes, there’s no need for one to be Python-savvy to formulate this program's code structure. So, let’s get coding right away!

I’m not a very conventional person, neither am I conservative, so I’m going to write a ‘Hello You’ program instead of a ‘Hello World.’ This simple program will take some information from the user and display it in subsequent lines.

To be most updated, I decided to replace my 3.8.x Python with the latest 3.9.1. It comes with an IDLE (Integrated Development and Learning Environment) that runs smoothly on my Windows 10 rig.

As for the Hello You program, this is how I imagined the final result should look like:

What is your name?: Peter
How old are you?: 37
In which city do you live?: Suburbia
What is your hobby?: Archery
Peter <class 'str'>
37 <class 'str'>
Suburbia <class 'str'>
Archery <class 'str'>

As you can see, I wanted the program to show the data type it takes in as inputs following each output. Regardless of each input's length, I wanted each entry’s data type to align perfectly with their counterparts on different lines.

By the way, all values from Python’s input() function will be stored as strings. What’s the point of showing the data type, then? Well, I just wanted the program to work that way. Totally arbitrary.

I started by creating a new script file from the IDLE and type in the following code:

#Hello You_v1.0
#User's information collection
name = input("What is your name?: ")
age = input("How old are you?: ")
city = input("In which city do you live?: ")
hobby = input("What is your hobby?: ")
#Print the information on screen (info and data type)print(name, type(name),
age, type(age),
city, type(city),
hobby, type(hobby))

The code has two main sections: the information collection section and the section that will put the information on the screen. They are just input and output sections, so to speak.

And it worked, not perfectly, but it worked:

What is your name?: Peter
How old are you?: 37
In which city do you live?: Suburbia
What is your hobby?: Archery
Peter <class 'str'> 37 <class 'str'> Suburbia <class 'str'> Archery <class 'str'>

The output was squeezed into one line. Something had to be done about it. So, I modified the code and gave birth to version 1.1 of ‘Hello You.’

#Hello You_v1.1
#User's information collection
name = input("What is your name?: ")
age = input("How old are you?: ")
city = input("In which city do you live?: ")
hobby = input("What is your hobby?: ")
#Print the information on screen (info and data type)
#Fine-tune the display with the escape character "\n"
print("\n",name, type(name),"\n",
age, type(age),"\n",
city, type(city),"\n",
hobby, type(hobby))

In 1.1, I tried to use the escape character “\n” so that each output will be on different lines. The program worked fine, but again, not perfect:

What is your name?: Peter
How old are you?: 37
In which city do you live?: Suburbia
What is your hobby?: Archery
Peter <class 'str'>
37 <class 'str'>
Suburbia <class 'str'>
Archery <class 'str'>

I was successful in putting the outputs on different lines, but wait! What are those spaces in front of each output line? They shouldn’t be there. And look at the spacing between the user information and the data type… What a mess!

I found out that Python’s print() function will always return an additional space unless you tell it not to. I tweaked the code again, adding a parameter to the print() function to hide its default separator and trying to get the spacing in order:

#Hello You_v1.2
#User's information collection
name = input("What is your name?: ")
age = input("How old are you?: ")
city = input("In which city do you live?: ")
hobby = input("What is your hobby?: ")
#Print the information on screen (info and data type)
#Quick-fix for spacing display (doesn't work well)
#Also, different capitalization consumes different amount of space
print("\n",name," ", type(name),"\n",
age," ", type(age),"\n",
city," ", type(city),"\n",
hobby," ", type(hobby), sep = '')

Adding the sep = ‘’ parameter will tell Python’s print() function not to add additional blank space in front of each output line. As for the messy spacing problem? I bluntly added 10 spaces between each output and its data type. The result was no less messy, as you can see:

What is your name?: Peter
How old are you?: 37
In which city do you live?: Suburbia
What is your hobby?: Archery
Peter <class 'str'>
37 <class 'str'>
Suburbia <class 'str'>
Archery <class 'str'>

The unwanted space in front of each line is successfully dealt with. User information and data type are separated by 10 spaces each, however, in a disorganized way still.

Separating user information and its data type can be simple enough, but I can assure you that it is not that simple. The following is to remind you what the result of this program should be:

Peter        <class 'str'>
37 <class 'str'>
Suburbia <class 'str'>
Archery <class 'str'>

I need the output to align in an orderly fashion as the above. This means that there must be some noticeable separation between the user information and the data type. Each line of data type must align with one another so that each data type begins at the same x position in its line.

The factor to consider to solve the spacing problem is the length of each piece of user information. The data type must begin after some certain spaces away from user information's longest entry. I determined that this space should be 10 taps of the spacebar.

Now, after contemplating some simple arithmetics, it dawned on me that the number of times I should tap on the spacebar to separate ‘Peter’ from its data type is (8−5) + 10 (eight minus five in closed brackets plus ten) times. What about the space between the age and its data type? Well, that’ll be (8−2) + 10 (eight minus two in closed brackets plus ten) times.

In this case, ‘city’ is the category that contains the longest string entry (Suburbia) with 8 characters.

At this point, it is obvious that the formula to calculate the necessary space to form an orderly-looking output is the length of the longest user information entry minus the length of any other user information entry plus ten, with ten being the arbitrary pre-determined space between the longest entry and its data type.

This formula must be generalized some more. That’s because I’m lazy and will not be typing unique sets of numbers in the code for each different length of user information entry. What if a user’s name is not Peter but Link? What if his city is not called Suburbia but Kokiri Forest?

For determining the proper space length for each entry, I came up with the following code:

#Hello You_v1.3
#User's information collection
name = input("What is your name?: ")
age = input("How old are you?: ")
city = input("In which city do you live?: ")
hobby = input("What is your hobby?: ")
#Print the information on screen (info and data type)
#Fixing the spacing problem by using more advanced solutions
#creating a set from user's information
userinfo = [name, age, city, hobby]
max_len = len(max(userinfo, key = len)) #get max length from userinfo
#use if condition to determine the right spacing for each line
space = ""
if len(name) == max_len:
for i in range(1, 5):
space = space + " "
else:
space_len = (max_len - len(name)) + 5
for i in range(1, space_len):
space = space + " "
print("\n",name,space,type(name),"\n",sep='')if len(age) == max_len:
for i in range(1, 5):
space = space + " "
else:
space_len = (max_len - len(age)) + 5
for i in range(1, space_len):
space = space + " "
print(age,space,type(age),"\n",sep='')if len(city) == max_len:
for i in range(1, 5):
space = space + " "
else:
space_len = (max_len - len(city)) + 5
for i in range(1, space_len):
space = space + " "
print(city,space,type(city),"\n",sep='')if len(hobby) == max_len:
for i in range(1, 5):
space = space + " "
else:
space_len = (max_len - len(hobby)) + 5
for i in range(1, space_len):
space = space + " "
print(hobby,space,type(hobby),sep='')

I made a list out of all the user information. Then I determined which element contained the longest string and how long it was. Then I used the length of the longest string to determine the rest of the spacings.

And the result I got is as the following:

Peter<class 'str'>37<class 'str'>Suburbia        <class 'str'>Archery        <class 'str'>

Obviously, something must have gone down very wrong… And I spent some time before figuring out that the problem was in the indentation. The else statements were not on the same levels as the ifs.

Every Python fan knows that the indentation level is an important grammar point. For Python, beautiful, orderly-looking indentations are not only for readability. Python indicates blocks of code from indentation levels. The same block of code, such as if… else… in this case, should be on the same level of indentation.

To be honest, I don’t know exactly why I got such an output when the else statements were not on the same level as the ifs. I moved on to fix this and got the following chunk of code…:

#use if condition to determine the right spacing for each line
space = ""
if len(name) == max_len:
for i in range(1, 5):
space = space + " "
else:
space_len = (max_len - len(name)) + 5
for i in range(1, space_len+1):
space = space + " "
print("\n",name,space,type(name),"\n",sep='')if len(age) == max_len:
for i in range(1, 5):
space = space + " "
else:
space_len = (max_len - len(age)) + 5
for i in range(1, space_len+1):
space = space + " "
print(age,space,type(age),"\n",sep='')if len(city) == max_len:
for i in range(1, 5):
space = space + " "
else:
space_len = (max_len - len(city)) + 5
for i in range(1, space_len+1):
space = space + " "
print(city,space,type(city),"\n",sep='')if len(hobby) == max_len:
for i in range(1, 5):
space = space + " "
else:
space_len = (max_len - len(hobby)) + 5
for i in range(1, space_len+1):
space = space + " "

…with the following result:

Peter        <class 'str'>37                   <class 'str'>Suburbia                            <class 'str'>Archery                                  <class 'str'>

Not a promising result, indeed. The simple spacing problem had become quite a challenge at this point.

Well, well, well, at this point, I really believed I was almost there, but this hiccup was literally ‘separating’ me further from my goal.

OK. After close examination of the 1.3 code body, along with the result’s characteristic, I realized that I should reset the space variable after each for loop. Without returning its original value, I’ll keep getting ever larger spaces for each entry down the stream.

I fixed the code with space = “” after every chunk of conditional and output operations as follows:

if len(name) == max_len:
for i in range(5):
space = space + " "
else:
space_len = (max_len - len(name)) + 5
for i in range(space_len):
space = space + " "
print("\n",name,space,type(name),"\n",sep='')
space = "" #reset the space variable
if len(age) == max_len:
for i in range(5):
space = space + " "
else:
space_len = (max_len - len(age)) + 5
for i in range(space_len):
space = space + " "
print(age,space,type(age),"\n",sep='')
space = ""
if len(city) == max_len:
for i in range(5):
space = space + " "
else:
space_len = (max_len - len(city)) + 5
for i in range(space_len):
space = space + " "
print(city,space,type(city),"\n",sep='')
space = ""
if len(hobby) == max_len:
for i in range(5):
space = space + " "
else:
space_len = (max_len - len(hobby)) + 5
for i in range(space_len):
space = space + " "
print(hobby,space,type(hobby),sep='')
space = ""

And I could tell from the result that I was really almost there!

Peter        <class 'str'>37           <class 'str'>Suburbia          <class 'str'>Archery      <class 'str'>

Almost, but obviously not there yet. The annoying odd space after Suburbia needed fixing. But what caused it?

I spent some more time checking the code for the possible cause of the odd space. I couldn’t find anything obvious. I decided to put in some extra lines of code to check what was really going on inside.

I decided to check the exact number of spaces the code provided to separate each user information from its data type. With print(len(space)) after each chunk of conditional and loop, some more clues were revealed:

if len(name) == max_len:
for i in range(5):
space = space + " "
else:
space_len = (max_len - len(name)) + 5
for i in range(space_len):
space = space + " "
print(len(space))
print("\n",name,space,type(name),"\n",sep='')
space = "" #reset the space variable
if len(age) == max_len:
for i in range(5):
space = space + " "
else:
space_len = (max_len - len(age)) + 5
for i in range(space_len):
space = space + " "

print(len(space))
print(age,space,type(age),"\n",sep='')
space = ""
if len(city) == max_len:
for i in range(5):
space = space + " "
else:
space_len = (max_len - len(city)) + 5
for i in range(space_len):
space = space + " "
print(len(space))
print(city,space,type(city),"\n",sep='')
space = ""
if len(hobby) == max_len:
for i in range(5):
space = space + " "
else:
space_len = (max_len - len(hobby)) + 5
for i in range(space_len):
space = space + " "
print(len(space))
print(hobby,space,type(hobby),sep='')
space = ""

This was how the pre-contemplated result looked like:

What is your name?: Peter
How old are you?: 37
In which city do you live?: Suburbia
What is your hobby?: Archery
8
Peter <class 'str'>11
37 <class 'str'>
10
Suburbia <class 'str'>
6
Archery <class 'str'>

The extra numbers above each user information showed how many spaces between it and its data type. All seemed right, except the problematic Suburbia. This category also happened to contain the longest string.

Does this problem happen only with any category with the longest string? Let’s find out!

I experimented by changing input lengths. And here’s the result:

What is your name?: Cthulhu
How old are you?: 4.543 billion years
In which city do you live?: R'lyeh
What is your hobby?: Slumbering
17
Cthulhu <class 'str'>5
4.543 billion years <class 'str'>
0
R'lyeh<class 'str'>
14
Slumbering <class 'str'>

Now, the longest entry of user information is not the city anymore. And there’s no problem with the longest entry because its data type aligns well with the other two. The problem still stays with the city category.

It’s good to know this. I managed to narrow down the problem and get closer to a solution with this experiment.

After focusing only on the chunk of code containing the city information, I found the problem and managed to fix it.

The code before fixing:

if len(city) == max_len:
for i in range(5):
space = space + " "
else:
space_len = (max_len - len(city)) + 5
for i in range(space_len):
space = space + " "

The code after fixing:

if len(city) == max_len:
for i in range(5):
space = space + " "
else:
space_len = (max_len - len(city)) + 5
for i in range(space_len):
space = space + " "
#print(len(space))
print(city,space,type(city),sep='')
space = ""

Did you see any difference?

It turned out to be a pretty silly problem. I just overlooked to correct the else statement's indentation the first time I realized I should do this.

I also decided to give the program a major facelift by eliminating the escape character ‘\n’ to make the result look tighter as a very ordered group of lines.

I also added separating lines as comments in the code body so that each major chunk of code is separated and can be clearly seen.

#Hello You_v2.0#User's information cllectionname = input("What is your name?: ")
age = input("How old are you?: ")
city = input("In which city do you live?: ")
hobby = input("What is your hobby?: ")
#Print the information on screen (info and data type)
#Fixing the spacing problem by using more advanced solutions
#creating a set from user's information
userinfo = [name, age, city, hobby]
max_len = len(max(userinfo, key = len)) #get max length from userinfo
#use if condition to determine the right spacing for each line
space = ""
#-------------------------------------------------------------------
if len(name) == max_len:
for i in range(5):
space = space + " "
else:
space_len = (max_len - len(name)) + 5
for i in range(space_len):
space = space + " "
#print(len(space))
print("\n",name,space,type(name),sep='')
space = "" #reset the space variable
#-------------------------------------------------------------------
if len(age) == max_len:
for i in range(5):
space = space + " "
else:
space_len = (max_len - len(age)) + 5
for i in range(space_len):
space = space + " "

#print(len(space))
print(age,space,type(age),sep='')
space = ""
#-------------------------------------------------------------------
if len(city) == max_len:
for i in range(5):
space = space + " "
else:
space_len = (max_len - len(city)) + 5
for i in range(space_len):
space = space + " "
#print(len(space))
print(city,space,type(city),sep='')
space = ""
#-------------------------------------------------------------------
if len(hobby) == max_len:
for i in range(5):
space = space + " "
else:
space_len = (max_len - len(hobby)) + 5
for i in range(space_len):
space = space + " "
#print(len(space))
print(hobby,space,type(hobby),sep='')
space = ""

And the result was just ‘perfect.’

What is your name?: Peter
How old are you?: 37
In which city do you live?: Suburbia
What is your hobby?: Archery
Peter <class 'str'>
37 <class 'str'>
Suburbia <class 'str'>
Archery <class 'str'>

There we (I) have it, the complete, real ‘Hello You’ program that works perfectly (as I imagined it should!) I achieved this by walking through the typical software development process, tweaking subtle, invisible errors in the code to fine-tune the result.

I feel that I’ve already shaken off a lot of rust from my Python tool, and I’ll proceed to keep it shiny moving forward. Wish me luck!

The Startup

Medium's largest active publication, followed by +771K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store