Hello! In this tutorial, we'll learn how to use a function in Python to calculate the return on an investment based on some initial information. Let's go!
Step 1: Defining the function
This line declares the function "calculate_profit" with three parameters: initial_investment, days_of_investment and guaranteed_return.
This line calculates the daily return that the person will have based on the guaranteed return and the number of days of investment. The value is stored in the "retorno_diario" variable.
This line calculates the total profit the person will make based on the initial investment and the guaranteed return. The value is stored in the "profit_total" variable.
This line calculates the daily profit that the person will have based on the initial investment and the previously calculated daily return. The value is stored in the variable "lucro_diario".
This line calculates the weekly profit that the person will have based on the previously calculated daily profit. The value is stored in the "weekly_profit" variable.
This line calculates the monthly profit that the person will have based on the previously calculated daily profit. The value is stored in the variable "profit_monthly".
These four lines print the results to the screen. Each line prints a text message with the calculated profit value. The {:.2f} part formats the number to display only two decimal places. The daily, weekly, monthly, and total profit values are populated in the appropriate places.
Step 2: Calling the function
Now that we have our function defined, we can call it to calculate the return on our investment. Let's use the following values as an example:
To call the function and calculate the profit, just run the following command:
This will print on the screen the daily, weekly, monthly and total profit of our investment based on the values we have defined.
Step 3: Interpreting the results
When running our function with the example values, we can see the following results on the screen:
This means that with an initial investment of R$1000, a guaranteed return of 126%, and an investment period of 30 days, we can expect a daily profit of R$3.31, a weekly profit of R$23.17, a profit monthly income of R$99.13, and a total profit of R$2262.60.
Creation of the graphical interface of the project
Let's now create a graphical interface that we will connect to this function, we will use tkinter for this.
The code below creates a GUI using tkinter.
For the icon use an image of your choice and you will also have to install the Python pillow library to manipulate the image size.
install the pillow using this command:pip install pillow
download the icons here: https://icons8.com/icons/set/profit
GUI code:
Connecting the function with the GUI
Now that we already have the graphical interface, let's connect the function to interact with the graphical interface, so the integration will be done as follows.
Go to the GUI after the logo, paste the following function, but first define the following:
This will help us to format currencies regardless of country.
Then paste the code:
I hope this tutorial was useful for you to understand how to use a function in Python to calculate the return on an investment. If you have any questions, just leave them in the comments!
0 Comments