-
Notifications
You must be signed in to change notification settings - Fork 5
/
Ex_Sheet_1_Num_2.py
65 lines (47 loc) · 1.42 KB
/
Ex_Sheet_1_Num_2.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
"""
Author: Gabriele Pompa
Identification Number: ABCXYZ [substitute with your Unisi Identification Number]
Date: 19-Mar-2020
File name: Ex_Sheet_1_Num_2.py
Description: This script scripts tests whether a number is even or odd.
"""
def isEven(n):
"""
Function isEven(n) tests whether number 'n' is even or odd.
It returns True is n is even and False otherwise.
Parameters:
n (int): number to test.
Returns:
even_flag (bool): flag, True if n is even and False otherwise.
"""
# `%` operator returns the remainder of an integer division.
# If the remainder of the division between `n` and `2` is 0, then n is even.
# The result of the operation n % 2 == 0 is a boolean value that we store
# into the variable even_flag
even_flag = (n % 2 == 0)
return even_flag
def main():
"""
Function main() tests whether two numbers n1 and n2 are even or odd.
Parameters:
None
Returns:
None
"""
# numbers to test
n1 = 10
n2 = 17
# test
is_n1_even = isEven(n1)
is_n2_even = isEven(n2)
# Print section
if is_n1_even:
print("n1 = {} is even".format(n1))
else:
print("n1 = {} is odd".format(n1))
if is_n2_even:
print("n2 = {} is even".format(n2))
else:
print("n2 = {} is odd".format(n2))
if __name__ == "__main__":
main()