Python Exercise: Character Input

So in my last post I introduced Python Programming Language as a high level programming language used for different purposes.

Here however, we will be doing an exercise I found on Practice Python, a fun site for near frustrating and almost simple Python exercises.

Let’s begin!

The Beginning

This exercise was supposed to be simple, theoretically, I thought it would be. I was taking my knowledge of Javascript into the Python space and I was working with so many assumptions.

So imagine my shock when I opened VScode to run Python and I just stared at the screen for 10 minutes racking my brain and trying to come up with a way to request user input (I did NOT want to go to Stack Overflow just yet).

I avoided this exercise for two days because I was annoyed at myself for not knowing how to collect user input (a little extreme, I know)

Understanding the exercise

The next step, was understanding the exercise itself

click image to view

The aim of the exercise was to get us, the exercise takers, to understand how to get user input, manipulate it by changing the data type and print it out.

In my head, I made this list for solving the exercise:

  • Step 1: Collect user input, age, and store it in a variable.
  • Step 2: Get the year of the current date and subtract the users age from it, this will give year of birth.
  • Step 3: Take the value from Step 2 and add 100 to it.
  • Step 4: Print out the year the user will turn 100 according to their input.

The Challenges

Challenge 1:

The first challenge I faced was in Step 2, extracting the year of the current date. Luckily for me, Python has a default date function which I could use to extract the current date and then strip down to the current year, so that’s what I used.

click image to view

Challenge 2:

The second challenge was also in Step 2, figuring out how to use the user’s input data.

The input () function in Python outputs its values as strings, and I needed to perform an arithmetic process on the data, and as we will eventually find out, string + integer = error, so I was a bit floored, but not for long.

Python has a clever way to convert data types and for this particular task, I only needed to convert the string value to an integer value using the int() function.

click image to view

 Conclusion

Once the challenges were done with, completing the exercise was a whole lot easier, you can view and download the full commented code here .

You can also view my raw thought process in pen and paper in the picture below and please let me know if this post at least encouraged you to keep on learning.

My scribbling

Remember, the site used in this is Learn Python, you can go to the site and give Exercise 1 a spin, send me your solutions at my personal email here, I’d love to see what you come up with.

Leave a comment