Sitemap

DIGITAL ACCESS PROGRAM USING PYTHON FUNCTION.

4 min readMar 8, 2023

--

A function in python is a block of codes which runs only when it is called. It makes it easier to reference/ call that block of code in an instance where it is needed within the program instead of repeatedly typing the same lines of code over and over again.

so, I decided to write a short digital access program using python function to show you how it works.

This program will allow a user enter his/her age, checks if the user is up to 18 years and then grant access, if less than 18 years and also 12 years and above, the program denies access and tells the user the number of years they have to wait before they can be eligible to access the platform, if less than 12 years, it denies the use access.

def access_prog():
'''this function grants or denies users access based on user age'''

age= 18
entry = input("How old are you? " )
dif = int(age) - int(entry)
if int(entry) >=int(age):
print("Access Granted")
elif int(entry) < int(age) and int(entry)>=12:
print("you're still a teenager, you have"+" "+ str(dif)+" "+ "years left to gain access to this platform.")
else:
print("you are too young, access denied!!")

access_prog()

CODE EXPLANATION

line1: defines the function using the def keyword and also assign the python identifier which in this case is ‘python_prog()’.

line2: this is a python function comment enclosed in triple quotation to give insight to another programmer who might have to work with it.

line3: declaring a variable ‘AGE’ setting the value to 18.

Get hanatu angud’s stories in your inbox

Join Medium for free to get updates from this writer.

line4: declares a variable ‘ENTRY’ which takes input from user.

line5: declares a variable ‘DIF’ which subtracts the value of variable ‘ENTRY’ from variable ‘AGE’. Take note that the variables values are been converted to integer (int) since we will be working with whole numbers.

line6: using the if statement we check if the value of ‘ENTRY’ is greater than or equal to the value of ‘AGE’ which is set to 18.

line7. If the condition in line6 returns true, then it should print ‘Access granted.’

line8: if the condition in line6 doesn’t hold, it checks if the value of ‘ENTRY’ is less than the value of ‘AGE’ or if the Value of ‘ENTRY’ is greater than or equal to 12. In essence, it checks if the value of entry falls between 12 and 17.

line9: If the condition in line8 returns true, then it should print the message in the parenthesis. Because I want the users in this age group to have an idea of how soon they will be eligible to access the platform, notice that we did a little string concatenation there by fetch the value of dif. so when the user enters that his age is probably 17 years, the value of dif which is set as the value of age less that of entry is fetched and the user is notified that he has 1 year left to gain access. The variable ‘DIF’ had to be converted to a string using ‘str’ in order to achieve this if not you will run into an error because concatenation can only be done on string.

line10: checks if ‘ENTRY’ doesn’t return true to either of the conditions above.

line11: If line10 is true, implying that none of the conditions in lines 6 & 8 are True then, it throws the message in parenthesis to the user.

line12: calls the function to run.

#def access_prog():
# '''this function grants or denies users access based on user age'''
# return
#age= input("whats your age?" )
age= 18
entry = input("How old are you? " )
dif = int(age) - int(entry)
if int(entry) >=int(age):
print("Access Granted")
elif int(entry) < int(age) and int(entry)>=12:
print("you're still a teenager, you have"+" "+ str(dif)+" "+ "years left to gain access to this platform.")
else:
print("you are too young, access denied!!")

# access_prog()

take note that the code above will still run perfectly even though the aspects of the function have been commented which no longer makes it a function. The advantage of using functions is that it allows you to write the code once and start referencing it all through the program instead of writing same code a million times at different instances within the program. With function coding can be less strenuous and easily understood by another programmer you might have to work with.

--

--

hanatu angud
hanatu angud

Written by hanatu angud

Angud - Data Scientist I facilitate clear and informed data-driven decision-making for organizations and build predictive models using machine learning.