# SCMA241 Lab : Functions

In :
# 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 :
# area and circumference of the circle (use math module)
import math

def area(radius):
# find area
area_circle = math.pi*radius*radius
return area_circle

def circumference(radius):
# find circumference
circ = math.pi*2*radius
return circ

def main():
r = float(input('Radius : '))
a = area(r)
b = circumference(r)

print(a)
print(b)

main()

Radius : 5
78.53981633974483
31.41592653589793

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

In :
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 :
# sum and multiply functions (with list as an argument)

In :
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 :
# is_member function

In :
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 :
# 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 :
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:
print('Your guess is too low.')
elif guess_val > true_val:
print('Your guess is too high.')
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
Your guess is too low.
Take a guess : 100
Your guess is too high.
Take a guess : 75
Your guess is too high.
Take a guess : 60
Your guess is too low.
Take a guess : 65
Your guess is too low.
Take a guess : 70
Your guess is too low.
Take a guess : 71
Your guess is too low.
Take a guess : 73
Your guess is too low.
Take a guess : 74
Good job,  Krikamol
You guessed my number in 9  guesses

In [ ]:
help(random.randint)

In [ ]: