In this tutorial, we are going to turn the structural engineering program we’ve been working on in the previous two tutorials into a command line program.
A command line program allows to separate where we write our code (PyCharm) from where we will run our code (the terminal). This concept is referred to as abstraction and is good practice when programming. We don’t need to see all of our code every time we run our program, and we don’t need prompts when editing our code.
This tutorial assumes you have worked through the last two tutorials. If you missed them, you can catch up here:
First, let’s deal with the number of decimal places we printed out last in our last tutorial. The result we output in the last tutorial included a large number of decimal places:
As mentioned in the last tutorial, Python classifies numbers with decimal places as a float data type.
Step 1: Clean up our result precision
Floats in Python have a default precision of 17 decimal places. This is typically well beyond the required level of precision for structural engineering. It’s good practice to reduce the decimal places to only what is required. For this we are going to need to use the round()
function.
Note: At a high level, functions take an input, perform some action, and return an output. We have already come across one function in the series so far, being the
print(“hello world!”)
function.To use or call a function in Python, you need to write the name of the function you want to use followed by circular brackets. Within the circular brackets you can add in inputs or arguments separated by commas. Arguments are values which are required for the function to work. It is possible to have zero arguments in which case you have empty circular brackets (writing
print()
will output an empty line) or multiple arguments for function.
For our round()
function, two arguments are required. The first is the number you’d like to round and the second is the number of decimal places. It’s written in the same way as Excel’s round function, with the cell reference replaced by a variable.
Our output is looking good, but it’s lacking what could be helpful information. Let’s add a description and units so we aren’t just printing a plain number that may confuse engineers who aren’t familiar with our program.
Here we’ve added additional arguments to our print function and it has joined or concatenated, using a single space, and output them all together. Nice!
Step 2: Add a dynamic bar diameter input
Next, we need to replace our hardcoded bar_diameter
input.
Note: The term hardcoded means we treated our
bar_diameter
variable as a constant. However, when we use the program we want to be able to change this value.
For this we are going to need another function we haven’t yet encountered, called the input()
function.
We made two new changes in the gif above.
First, we added our input()
function and assigned it to variable. What’s happening here? Similar to the lines of code performing calculations, the right hand side of the equal sign is evaluated before it is assigned to the variable on the left hand side. Given we know that functions return a value, the input()
function is going to evaluate first and return a value that will be stored in the bar_diameter
variable.
Second, we made a conversion using another function: the int()
function. 🤔 This one might be easier to explain once we run our code, so let’s do that:
Now when we run our code, we don’t see a result but rather an instruction:
Enter bar diameter:
The input()
function here is pausing the execution of our code. It’s reaching this line and waiting for us to enter an input in the terminal. Its argument, “Enter bar diameter:” is output in the terminal as an instruction. Once we input our bar diameter we can press enter and the input()
function will take the value and store it in our bar_diameter
variable. It will then continue the execution of our program, and eventually output our result.
Now we can analyse the linear mass of any reinforcement bar using its diameter. Congratulations, you just completed your first structural engineering program in python 🥳!
Step 3: Wrapping up
I still haven’t told you about this mysterious line:
bar_diameter = int(bar_diameter)
When we enter the 20
into the input function, Python is not aware that it should treat that value as a number. By default, all inputs are stored as a string data type. We need to tell Python that we want the value we input to be treated as a number. This line allows us to convert the data type of the bar_diameter
variable from a string
to an int.
If you are brand new to programming this might seem very weird, but over time data types will become second nature to you.
Our last clean up item brings us back to the beginning: running our new structural engineering program from your terminal rather than in our IDE (PyCharm). You simply copy the main.py
file path and write the python3
command and paste the path to your new program.
But what if you want to impress another engineer who think your new program would be super helpful? You can simply zip your newly created program and email it through to them. Once they’ve downloaded Python (like we did in our first part of this course), they can unzip the file and run the program on the command line just as we did above.
And that’s it for this series! You made it! You are now an incredible structural engineering coder and will be able to pass on your knowledge to others.
If you’ve enjoyed this course, keep your eyes peeled for another course to be released in the coming weeks.
🤫 Here is a sneak preview of what we’ll be building next time:
How did we go in this part of the series? Let us know below!
Hello Nick, What about the upcomming sessions, any update.
i am stuck at the last step the pychram code won't run in the terminal if i copy paste it says error invalid syntax and if i use open in terminal option from pycharm it says running scripts is disabled on this system