# SCMA241 Lab : Functions

In [2]:
# template of the program

# function 1
def function_1():
print('function 1')

# function 2
def function_2():
print('function 2')

# function 3
def function_3():
print('function 3')

# main function
def main():
print('Main function')
function_1()
function_2()
function_3()

# call the main function
main()

Main function
function 1
function 2
function 3

In [3]:
# area and circumference of the circle (use math module)
import math

# find area
return area_circle

# find circumference
return circ

def main():
a = area(r)
b = circumference(r)

print(a)
print(b)

main()

Radius : 5
78.53981633974483
31.41592653589793

In [4]:
# min and max functions (with list as an argument)

In [5]:
def min_fun(numbers):
# find minimum
min_val = math.inf

for i in numbers:
if i < min_val:
min_val = i

return min_val

def main():
list_numbers = [9,5,3,4,6,9,12,4,1]
min_number = min_fun(list_numbers)

print(min_number)

main()

1

In [6]:
# sum and multiply functions (with list as an argument)

In [7]:
def sum_fun(numbers):
# find sum
sum_val = 0.0

for i in numbers:
sum_val += i

return sum_val

def main():
list_numbers = [9,5,3,4,6,9,12,4,1]
sum_value = sum_fun(list_numbers)

print(sum_value)
print(sum(list_numbers))

main()

53.0
53

In [8]:
# is_member function

In [9]:
def is_member(numbers,n):
# find if n is a member of numbers
is_mem = False

for i in numbers:
if i == n:
is_mem = True

return is_mem

def main():
num = 12
list_numbers = [9,5,3,4,6,9,12,4,1]

if num in list_numbers:
print('YES!')
else:
print('NO!')

main()

YES!

In [10]:
# a guessing game : a user enters one number. The program must tell whether it is smaller or larger than the true number. The program terminates when the user enters the correct number. [while loop]
# ** break into multiple functions:
# ***** get playerâ€™s name
# ***** get input number
# ***** check the input number

# Hello! What is your name?
# Krikamol
# Good afternoon, Krikamol, I am thinking of a number between 1 and 100.
# Take a guess.
# 10
# Your guess is too low.
# Take a guess.
# 15
# Your guess is too low.
# Take a guess.
# 18
# Good job, Krikamol! You guessed my number in 3 guesses!

In [1]:
import random

def get_name():
# get player's name
name = input('Hello, what is your name? ')
print('Good afternoon, ', name,', I am thinking of a number between 1 and 100.')
return name

def take_guess():
# take a guess
num = int(input('Take a guess : '))
return num

def check_number(true_val,guess_val):
# check the guessed number
is_end = False

if guess_val < true_val:
elif guess_val > true_val:
else:
is_end = True

return is_end

def main():
# guess a number game
true_number = random.randint(1,100)
player_name = get_name()
guess_number = take_guess()

num_of_guess = 1

while (not check_number(true_number,guess_number)):
guess_number = take_guess()
num_of_guess += 1

print('Good job, ',player_name)
print('You guessed my number in', num_of_guess,' guesses')

main()

Hello, what is your name? Krikamol
Good afternoon,  Krikamol , I am thinking of a number between 1 and 100.
Take a guess : 50
Take a guess : 100
Take a guess : 75
Take a guess : 60
Take a guess : 65
Take a guess : 70
Take a guess : 71

help(random.randint)