In problem 1 we will practice how to define very simple functions in order to remind ourselves about the basic syntax (input parameters, indentation, return value). In problem 2 you are asked to create a simple tool that converts temperatures from one temperature-type to another. After making your changes, remember to upload the files to GitHub.
Create a script file my_functions.py and define the following functions in it:
- A function
congrats(name)
which takes a string variablename
as input and prints out "Onnea nimi!" when called.
Your code should look something like this:
def congrats(name):
#Define below the print command. Pay attention to indentation!
Usage should look like this:
In[]: congrats('Henkka')
Out[]: 'Onnea Henkka!'
- A function
BBoxArea(side_a, side_b)
which which takes two numerical parametersside_a
andside_a
representing the length of bounding box edges and calculates the area (in square units) based on these input parameters and returns the area.
Usage should look like this:
In[]: BBoxArea(30,30)
Out[]: 90
- A function
circleArea(radius)
which takes circle radius as input and returns the circle area.
Usage should look something like this:
In[]: r = 10
In[]: print('The area for a circle with a radius of ' + str(r) + ' mm is ' + str(circleArea(r))+' mm^2')
Out[]:'The area for a circle with a radius of 100 mm is 31415.926 mm^2'
You can also try what happens is you call this command:
for x in range(5):
print(circleArea(x))
...:
The task is to create a temperature calculator that is used for converting temperatures between Celsius, Fahrenheit and Kelvin. You are asked to modify and add functionality to the functions in temp_converter.py -file. The temp_converter.py -script is broken in its current state, thus we want you to fix it by going through the tasks denoted with numbers in the script (1-4). The comments in the script will guide you, and instruct what to do in different parts of the code. There are missing parts in the code denoted with XX letters. Modify the script and add your own code to places where letters XX are present.
In the script you have 6 functions for converting temperatures between different types. Templates for these functions are pre-filled in the script but you need to modify them (three of them).
We provide you links to materials where you can find the correct mathematical formulas to convert temperatures between different types.
There is also one function ( temp_calculator() ) that works as a simple user controller that should take 3 parameters as input (you need to modify and add your own code to the function):
- temp = parameter for passing temperature (numerical)
- convert_from = parameter that determines whether the input temperature is in Celsius, Fahrenheit or in Kelvin (using letters "C", "F" or "K" accordingly)
- convert_to = parameter that determines whether the output temperature is in Celsius, Fahrenheit or in Kelvin (using letters "C", "F" or "K" accordingly)
At the end of the script there are three use cases where different functionalities of the script are tested. If everything in the script is working properly these test cases should produce following outputs:
>>> print("32 Fahrenheits is in Celsius:", fahr_to_celsius(32))
32 Fahrenheits is in Celsius: 0.0
>>> print("100 Celsius is in Kelvin:", celsius_to_kelvin(temp_c=100))
100 Celsius is in Kelvin: 373.15
>>> print("50 Fahrenheits is in Kelvin:", fahr_to_kelvin(temp_f=50))
50 Fahrenheits is in Kelvin: 283.15000000000003
>>> temperature = 30
>>> converted_temp = temp_calculator(temp=temperature, convert_from="C", convert_to="F")
>>> print("Temperature %s in Celsius is %s in Fahrenheit" % (temperature, converted_temp))
Temperature 30 in Celsius is 86.0 in Fahrenheit
>>> temperature = 50
>>> converted_temp = temp_calculator(temp=temperature, convert_from="K", convert_to="F")
>>> print("Temperature %s in Kelvin is %s in Fahrenheit" % (temperature, converted_temp))
Temperature 50 in Kelvin is -369.66999999999996 in Fahrenheit
>>> temperature = -20
>>> converted_temp = temp_calculator(temp=temperature, convert_from="F", convert_to="C")
>>> print("Temperature %s in Fahrenheit is %s in Celsius" % (temperature, converted_temp))
Temperature -20 in Fahrenheit is -28.88888888888889 in Celsius
Remember to upload your updated temp_converter.py -script to your personal GitHub repository after major changes!
Note:
We hope that you can get the whole script working but it is not the end of the world if you cannot fix all of its functionalities. The main point in this exercise is that you get a first experience of how more "proper" programs can be created by taking advantage of functions.
In addition to providing your updated temp_converter.py script, we wish you to think and answer to following questions based on the materials and ideas that you learned during the lecture:
- Is the concept of function clear to you? If not, what do you not understand?
- What are the benefits of using functions in your script?
- Does it matter in which order the functions are written in a script? If you think it does, why?
If you wish to dive deeper into functions and geometric objects in Python, you can have a look at materials from the extensive version of the Automating GIS processes course: