-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate_example.py
134 lines (94 loc) · 3.6 KB
/
template_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"""
Author: Gabriele Pompa
Identification Number: ABCXYZ [substitute with your Unisi Identification Number]
Date: 04-Mar-2020
File name: template_example.py
Description: This script is a template to be followed by any script that you define.
In this example we take two integer numbers, add them and then multiply them.
Finally we print them on screen.
"""
def powNum(a,b):
"""
Function powNum(a,b) exponentiates number 'a' to the power 'b' and returns the result as output 'c'.
Parameters:
a (int; float)
b (int; float)
Returns:
c (int; float)
"""
# calculation of a^b and assignment to variable c
c = a**b
# output c
return c
def mulNum(a,b):
"""
Function mulNum(a,b) multiplies number 'a' and 'b' and returns their product as output 'c'.
Parameters:
a (int; float)
b (int; float)
Returns:
c (int; float)
"""
# calculation of a*b and assignment to variable c
c = a*b
# output c
return c
def addNum(a,b):
"""
Function addNum(a,b) adds number 'a' and 'b' and returns their sum as output 'c'.
Parameters:
a (int; float)
b (int; float)
Returns:
c (int; float)
"""
# calculation of a+b and assignment to variable c
c = a+b
# output c
return c
def main():
"""
This is a docstring (DOCumentation STRing).
Always add documentation strings to the functions the you define.
Documentation strings should explain what the function does, its inputs and its outputs.
Input and output of each function should be described in details,
using the "Parameters" and "Returns" sections of the docstring, using the format:
variableName (variableType)
like:
x (int)
If more than one varible type is compatible with the function, separate all the possible variable types using a ';', like:
x (int; float)
If there are no inputs or if the functions doesn't return anything, just write "None",
as in the case of main() function.
Function main() is the main function of the code and it is used to manage the high-level flow of the code,
in particular, it manages:
- the definition of variables,
- the access to data (when necessary),
- the calls to other more specific functions,
Here below is how a possible docstring for function main() might be written:
Function main() defines two variables 'x' and 'y' and computes their sum, product and exponentiation.
Finally, it prints on screen the results.
Parameters:
None
Returns:
None
"""
# variable definition
x = 17
y = -1
# addition x+y calling function addNum(a,b) and assignment to variable z
z = addNum(x,y)
# product x*y calling function mulNum(a,b) and assignment to variable w
w = mulNum(x,y)
# exponentiation x^y calling function powNum(a,b) and assignment to variable p
p = powNum(x,y)
# Print section
print("Sum: x+y=({})+({})={}".format(x,y,z))
print("Product: x*y=({})*({})={}".format(x,y,w))
print("Exponentiation: x^y=({})^({})={}".format(x,y,p))
if __name__ == "__main__":
# Python interpreter accesses your code from this block
# Remember to document possibly each line of code, explaining what it does and why you decided to do that
# To run code press "Run" in the "Run" section of Spyder's top panel.
# call function main()
main()