Functions II: Monday, September 20, 2021 Cs4051 Fundamentals of Computing 1
Functions II: Monday, September 20, 2021 Cs4051 Fundamentals of Computing 1
Functions II
Sukrit Shakya
[email protected]
def subtract(a,b):
return a-b calculate the value for x
def multiply(a,b):
return a*b
def divide(a,b):
return a/b
def subtract(a,b):
return a-b calculate the value for x
first = add(5,9)
def multiply(a,b):
second = subtract(5,9)
return a*b
x = multiply(first,second)
def divide(a,b): print(x) #prints out -56
return a/b
def subtract(a,b):
return a-b calculate the value for x
x = multiply(add(5,9),subtract(5,9))
def multiply(a,b):
print(x) #prints out -56
return a*b
def divide(a,b):
return a/b
def subtract(a,b):
return a-b calculate the value for x
first = add(5,9)
def multiply(a,b):
second = subtract(5,9)
return a*b
third = multiply(first,second)
def divide(a,b): x = divide(third,2)
return a/b
print(x) #prints out -28
hello python
open function to open file filename mode in which the file
this is a file
in being opened, “r”
print(file.read()) means reading mode
file.close()
>>>
['hello python\n',
'this is a file']
print(line)
file.close()
>>>
hello python
this is a file
for i in range(3):
write the current value of i to file
file.write(str(i)) using the write function, i is of int
type so need to convert to str before
writing to file
file.close()
for i in range(3):
write the current value of i to file
file.write(str(i)) using the write function, i is of int
file.write(“\n”) type so need to convert to str before
writing to file
CS4051 FUNDAMENTALS OF
Monday, September 22
Testing example
• Suppose you are testing a function created for adding 2 numbers
Test no. 1
Action Pass two numbers 2 and 3 as
parameters to function add_two
Expected output 5
Actual output 5
Test result Pass
Test no. 3
Action Pass two numbers 2 and 1 as parameters
to function add_two
Expected output 3
Actual output 1
Test result Fail