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")

Python Lesson 3, Reflection

Here are two videos on the process that we went through today (they are screen recordings of my computer throughout the coding lesson, so that you can see exactly what I did throughout the lesson)

Reflection:
Today’s lesson was really fun in general, where we were able to learn a lot about pseudo code, and  more about different statements that we can use. We found the importance of flow charts, and did a fun exercise on how to feed a dog using one. After, we started to start the game production of “Guess the number”. This small game combines all the skills that we learned so  far into a fun entertaining game. I’m excited to see the final product later on!
-If, While, For in range statements
Statements are crucial to any part of programming. Here is a short review of each statement we learned in this class.
If- these statements are the one corresponding to the first true condition, or if all conditions are false, then it will go to the final end line.
While- The condition keeps looping as long as the while is true.
 Image result
For- In this specific  example we used the for statement as in if i is in this given range, then we can print this value “i”.
vnumber1= 5
vnumber2= 2
if vnumber1 > vnumber2 :
    print (vnumber1, "is bigger")

vnumber3= 0
while vnumber3 <10:
    print (vnumber3)
    vnumber3 = vnumber3+ 1
for i in range (0,20,2):
    print (i)

vnumber4=  10
while vnumber4 < 6:
    print (vnumber4, "is less than 6")
-The need for planning, using flow charts and pseudo code
Flow charts- Flow charts are used to help map out different loops, and are crucial in programming for many reasons. For the loop for “how to feed a dog” here is the two functions we used.
Decisions- Shown as a diamond- this shows the true or false, in which if it is false then it loops to somewhere, and if it is true, something else happens.
Process- Shown as a rectangle- this shows the process, so simple actions, usually the result of a decision, but can be just simple an action that occurs in the code.
What is a pseudo code?
Pseudo code is used to communicate what the computer is doing, using words and sentences. These occur in # because they are telling the reader what to do, rather than the computer what to do. The pseudo code is a way for coders to communicate what their code does. It is alike to the speech of the code. In other words it is a notation of the simplified programming.
-Include the ‘feed your pet’ algorithm.
We tried to apply both the pseudo and the flow  charts to our own little example. This was using the example on how to feed a dog. Sonia and I first started with making a flow chart, and soon proceeded in creating a pseudo code for it.
WhatsApp Image 2016-10-07 at 11.32.00 AM WhatsApp Image 2016-10-07 at 2.53.44 PM
The beginning of the game making is really interesting so far. After making a pseudo code for this, we managed to have a plan on how to make the code. The parts we have tackled so far is creating a random number generator, in which the computer will generate a number and you will have to guess the number using the guesses that the computer gives you.
Here is the code that we have so far.
import rand.num(1,50)
print("I chose a random number between 1-50, can you guess the number I chose? ")
vnumberguess1= input ("You have five guesses. What is your first guess?")


 Here is the code that we did in this entire class.
#If vnum1 is less than vnum2 then, print vnum 1 is smaller
#pseudo code - comment something, comment, speech, thought process
#while count is less than 6, then  print count is less than 6
# If it says "if" then it is an if statement
vnumber1= 5
vnumber2= 2
if vnumber1 > vnumber2 :
    print (vnumber1, "is bigger")

vnumber3= 0
while vnumber3 <10:
    print (vnumber3)
    vnumber3 = vnumber3+ 1
for i in range (0,20,2):
    print (i)

vnumber4=  10
while vnumber4 < 6:
    print (vnumber4, "is less than 6")
#i=0
#vname1 = input ("What is your name?")
#print (vname1)
#for i in range (vname1[0-12]):
    #print (i) this doesn't work
    #for each letter in "your name" print  each letter
#how to do it?
vname6= input ("What is your name? ") #characters of the name, calling for each
vletter= vname6
print (vletter)
i=0
for letter in vname6:
    print (vname6 [i])
    i=i+1
# vname7= input ("What is your name? ")
#vletter1= vname7
#print (vletter1)
#i=0
#for letter in vname7:
    #print (vname7 [:]) #this means that it prints your name, as many times as there are letters in your name
    #i=i+1
print ("I chose a random number between 1-50, can you guess the number I chose? ")
vnumberguess1= input ("You have five guesses. What is your first guess?")

import rand.num(1,50)
print("I chose a random number between 1-50, can you guess the number I chose? ")
vnumberguess1= input ("You have five guesses. What is your first guess?")

Python Lesson 1, Reflection

Here is a video of my code so far. (It shows how it works so far)
https://www.youtube.com/watch?v=n4qRPs7KvlY&feature=youtu.be
First parts of the coding process
Screen Shot 2016-10-03 at 2.12.08 PM Screen Shot 2016-10-03 at 2.12.19 PM
For the worksheet
Screen Shot 2016-10-03 at 3.05.39 PM
Lesson objectives:
  • Learn how the print function works
  • Defining a variable
  • Using an input function
  • The differences between ‘Integers’ and ‘strings’

Today was the first day of coding, and we started to learn about how to print functions, as well as integers and strings. By doing this, it was a nice introduction to the entire programming unit, and we were able to make a timestable. By making a working timestable (so by asking the user which number timestable they wanted to learn, and by pressing enter having the program write the timestable until 12)

The print function works by writing print, then brackets.

After you write the brackets you could perform two different tasks. One is that you could start off with quotation and ask the program to “speak”. Or you can put a equation eg. 5+3, and ask the program to perform this task.

A variable in programming is a container. You can put the variables into the program as storage containers, to store either words, or numbers.

An input function is basically where they ask the user to input an answer to the prompt. For example, variabled=input (“Where is the kitchen?”) This input would allow us to enable the audience to be able to respond to the prompt.

Integers and strings are different in many ways. A string is for storing words, where integers are a number that the computer recognizes as a number.

I found today’s lesson really helpful, as it was a nice easy way into programming as a whole. By learning more about the strings, and integers, we could apply these to make a simple task. I hope that by learning these simple tasks I can apply them in the future to more complicated tasks.