Python Lesson 5- Reflection

The benefits of using Functions:
The benefits of functions are endless, and they can help with many different aspects. The first is that it allows you to repeat lists endlessly. They are required to organise, to reuse code, and to perform single, related action. Functions are used to high degree of repeating. Furthermore, it is also possible to create your own functions.
Defining a function: Functions can be defined with the keyword def, followed by the function name and parentheses. This can later be called up to print, or to do other things, and be repeated as well. However, defining a function only gives it a name, but it’s when you put something into the parentheses does the function have a use. This function then can be used to print. By creating functions, you can use it at any time, at any place, and saves time in terms of you don’t have to keep telling the computer what to do over and over again. Functions come either premade, or you can define your own functions. The pre-made functions can then be used by simply “calling” the function.
def f_sum (int1,int2):
    """this function will add numbers"""
return int1 + int2
#main program
vTotal= f_sum (10,15)
print ("the total is: ", vTotal)

 

The function is useless without being called. But once you call it, you can print it, and define the function as well.

Start of the Chocoltae Machine Challenge.

 The chocolate machine challenge was a really interesting task, and enabled us to put into action some of the things we learnt in previous classes, as well as today. It was interesting to see how we could create code that could actually be used in the real world. Although it is simplistic to many people, it feels accomplishing to know that we are actually at a stage of coding where we can make solutions to real life problems. The chocolate machine challenge was where the machine calculates how much change is received by the person paying. We all started off with making a basic code and adding to it as we continued. I faced problems where we tried to make the code understand that when the given amount of money is less than the needed amount, then the code writes something. We are still working on this part.

For our machine, the next steps is trying to make the code have some sort of function included. Since functions are quite new to everybody, we were all skeptical of using it. I think next time, if we continued to work on the chocolate machine,we could definitely try to incorporate more functions into our machine.

 

Reflection: Overall I really enjoyed today’s class, and think that it was really beneficial. We learned about the uses of functions, and how we can apply it to real situations. It is also interesting to see how all the code we have learnt so far can be used to create simple solutions to problems that may actually occur in real life.

#functions are significant in many ways

# Functions can reduce our program code significantly by putting repeat code into a function
#modules
# Calculations and logical things you have to do--> the better solution is to write a function. You can
# make functions to have infinitely repeats of something.
def f_sum ( int1, int2):
    """This function will add """
#learn how to print on separate lines
# create your own multiple line message here:
print ("""
HI!
""")

def i2P_intro():
    print ("""
HI!
""")

print (i2P_intro())

#what a function does #functions must begin with a letter and not contain spaces
def f_sum (int1,int2):
    """this function will add numbers"""
return int1 + int2
#main program
vTotal= f_sum (10,15)
print ("the total is: ", vTotal)

# prints the total amount of the integers given
#the functions are stored at the top. I needs to store the program first, then make the input underneath, and do something with the variable
#Set the variables first
#Why is there a none at the beginning ?
# this is because the function is not yet used.
#returning values
def f_print_largest (int1, int2):
    """"This function will print the largest integer"""
if int1 > int2:
        print (int1, "is the largest")

    if int1 < int2:
        print ( int2 ," is the largest")

    if int1 == int2 :
        print ( "both numbers are" ,int1, "they are equal")

        #now it is returning, if it is equal the return, the largest of two integers"
def f_larger (int1, int2 ):
        """This function will return the largest of two integers"""
if int1 >= int2:
            return int1

        else:
            return int2

x=12
y=7
z=79
print(f_larger(f_larger(x,y),z))

#int1 = input (" Choose a number ")
int2 = input ( "Choose a number")

f_print_largest(int1,int2)
print ( f_print_largest)

#main program
#enter the price of the chocolate bar in HKD ( 16.90 = $16.90)
price= 16.90
print ("The price of a chocolate bar $16.90")
vcash= float ( input ("How much are you going to pay?"))
vinput = ("please pay again ")
Cashdue= int(vcash) -price
print ( "You change is ", Cashdue,"dollars ")
while vcash != price:
    if vcash > price:
        print ( "You change is ", Cashdue,"dollars ")
    if vcash < price:
        print (" Please pay", price)


In addition: ( this is what we learned in class the next lesson) In this lesson we not only finished our candy machine game, 
but also learned more about functions and their different applications.
# def f_ask_yes_no (question):
#     """"Ask a Yes or No question"""
#     vResponse= None
#     while vResponse not in ("yes", "no"):
#         vResponse= input (question).lower()
#     return vResponse
#
# vQuestion= " Would you like a Mars Bar? "
# vAnswer= f_ask_yes_no(vQuestion )
# if vAnswer =="yes":
#     print ( "here is a Mars Bar, I hope you enjoy it")
# if vAnswer== "no":
#     print ( "No Mars Bar for you ")
#
#
# def menu (question):
# #
# #
# # def print_menu():       ## Your menu design here
# #     print 30 * "-" , "MENU" , 30 * "-"
# #     print "1. Menu Option 1"
# #     print "2. Menu Option 2"
# #     print "3. Menu Option 3"
# #     print "4. Menu Option 4"
# #     print "5. Exit"
# #     print 67 * "-"
# #
# # loop=True
# #
# # while loop:          ## While loop which will keep going until loop = False
# #     print_menu()    ## Displays menu
# #     choice = input("Enter your choice [1-5]: ")
# #
# #     if choice==1:
# #         print "Menu 1 has been selected"
# #         ## You can add your code or functions here
# #     elif choice==2:
# #         print "Menu 2 has been selected"
# #         ## You can add your code or functions here
# #     elif choice==3:
# #         print "Menu 3 has been selected"
# #         ## You can add your code or functions here
# #     elif choice==4:
# #         print "Menu 4 has been selected"
# #         ## You can add your code or functions here
# #     elif choice==5:
# #         print "Menu 5 has been selected"
# #         ## You can add your code or functions here
# #         loop=False # This will make the while loop to end as not value of loop is set to False
# #     else:
# #         # Any integer inputs other than values 1-5 we print an error message
# #         raw_input("Wrong option selection. Enter any key to try again..")
#



vChoice= None
while vChoice != "0":
    print (
        """
        Menu Choice
        0- Exit
        1- Item 1
        2- Item 2
        3- Item 3
        4- Item 4
        """
)

    vChoice =input ("Choice:")
    print ()

    #exit
if vChoice == "0":
        print ("Good-bye")

    #Choice 1

elif vChoice =="1":
        print ( "Choose Item 1 ")

    #Choice 2

elif vChoice =="2":
        print ( "Choose Item 2 ")

        #Choice 3
elif vChoice =="3":
        print ( "Choose Item 3 ")

        #Choice 4
elif vChoice =="4":
        print ( "Choose Item 4 ")

    else:
        print ("Your choice is invalid")

Leave a Reply