My first Python application – and it’s not “Hello World”

Writing your first program, that isn’t “Hello World”, need not be hard. Whether Python is your first or fifth language, it need not matter. I’m a keen believer that you should practice what you learn to ensure it sticks, so you can remember it for years to come. I do this with everything I learn, not just code.

I’m still near the very beginning of my Python and Programming career (check out this post if you want to know more about my background). But I still feel it is important to share what I learn with you for a few reasons. Firstly, you can leave me a comment and show me how something might be done differently, or better. Secondly, it helps me reinforce the knowledge I’ve gained.

“To teach is to learn twice” — Joseph Joubert

There is also the possibility that you found yourself on my blog because you are a beginner in Python and/or programming, too, and are hoping to learn something. In which case, welcome.

My first app was a simple Tip Calculator. Something I need, being a British expat living in the U.S. This is just a straight forward console app, which of course is useless in a “real world” situation. But it helped me nail down a few basics without learning GUI (just yet).

There is one more issue with this, aside from the lack of GUI. That is the % to tip is a preset amount. The next iteration of this I will ensure that the user can input how much they want to tip, based on the level of service they were given. Keep your eye out for future posts for further iterations of this. The code can also be found on my GitHub here. Oh, and I’m using Python 3.4.

Getting started

First things first, let’s define a few functions that we’ll call later on. As I mentioned a minute ago, I’ll be using a fixed tip amount for this app. So let’s start by defining a function tip_amount which takes just one argument meal. Brits are known for over-tipping, but I’ve been here long enough to know 15% is standard. So I stuck with that. The function returns the variable tip, unsurprisingly. Here’s how it looks:

Now let’s work out how much the bill will cost with the added 15% tip. If you haven’t already figured it out, the meal variable will be defined later, when the user tells us the cost of the bill before tip. This functions is very similar to the previous.

Getting user input

It’s time to get the user to input the cost of the bill. I’ve done so simply by calling the input() method. Because the user is inputting the “something”, it will automatically be treated as a string by Python. But we don’t want that. We are looking for a float number, like 15.00. So we need to wrap the input() method with the float() method so that the input is converted to type float.

Formatting the outputs

I could have printed the outputs as they are, as floats. People would understand it. But I wanted to make it a little “prettier”, and learn a little more. So I decided to turn the floats into currency formats. I started with two new variables, dol_tip and dol_total. I will then call the format() method and pass in the tip_amount and final_total functions as the arguments. To convert it to a dollar format with two decimal places you need to use the syntax '${:,.2f}'. So that looks like this:

Final prints

So let’s actually tell the user what the tip will be, and what the final bill total will be. These are simple print statements which pass in the variables dol_tip and dol_total.

Bringing it all together

Hopefully this all made sense, even if you’re new to programming. I’m positive there is more than one way of doing it, so please do let me know if you can make this code cleaner.

Hopefully I’ll get chance to work on the next iteration soon. You can keep updated via my new twitter dev account @RWhiteDev.

I’ll leave you with the code in full. Enjoy.

Leave a Reply

Your email address will not be published. Required fields are marked *