Lesson 5 Input

#function enables you to do things repetition or infinite number of times. print(""" Line 1 Line 2 Line 3 """) #Create your own multiple lined message here: print(""" HKIS.. Intro to Programming ... with Ms. Mok! """) #Now try putting this into a function to print off automatically when you need it without repetition def i2p_intro(): print(""" HKIS.. Intro to Programming ... with Ms. Mok! """) print (i2p_intro()) ##Functions - Functions must begin with a letter and not contain spaces - variables/functions must be set first def f_sum(int1,int2): """This function will add two numbers""" return int1 + int2 #main program vTotal=f_sum(10,15) print("The total is:", vTotal) #returning values function int1 = input("Please choose your first number") int2 = input("Please choose your second number") def f_print_largest (int1,int2): """This function will print the largest of two integers""" if int1 < int2: print(int1, "is the largest") if int1 > int2: print(int2, "is the largest") if int1 == int2: print(int1,"and", int2, "are both equal") #main program f_print_largest(int1,int2) def f_larger (int1,int2): """This function will return the largest of the two integers""" if int1 >= int2: return int1 else: return int2 #main program x = input("Please input your first number") y = input("Please input your second number") z = input("Please input your third number") print((f_larger(f_larger(x,y),z)), "is the larger one")

Lesson 5 Output

Chocolate Machine Input

print("The price of the chocolate bar is $16.90.") vCash = input("How much are you going to pay?") vChange = int(vCash) - 16.90 print("You are getting $",vChange, "back") #vTen is the number of ten dollar bills you have to pay vTen=int(vChange/ 10) print("I am giving you back",vTen,"ten dollar bills.") #vFive is the number of five dollar bills you have to pay vFive=int((vChange-(vTen*10))/5) print("I am giving you back",vFive,"five dollar bills") #vOne is the number of one dollar bills you have to pay vOne=int(vChange-(vTen*10)-(vFive*5)/1) print("I am giving back",vOne,"one dollar bills") #vCent is the number of cents you have to pay vCent=int((vChange-(vTen*10)-(vFive*5)-vOne)*10) print("I am giving back",vCent,"cents")

Chocolate Machine Output

Lesson 6 Input:

#Now write a new function that takes two numbers as parameters (inputs ) #and prints out the smallest of the two int1 = input("Please choose your first number") int2 = input("Please choose your second number") def f_print_smallest (int1,int2): """This function will print the smallest of two integers""" if int1 < int2: print(int1, "is the smallest") if int1 > int2: print(int2, "is the smallest") if int1 == int2: print(int1,"and", int2, "are both equal") f_print_smallest(int1,int2) #Now write a function that takes no parameters or returns no values but just prints out "Ms Mok Smells" 3 times def print_function(): print("Ms Mok smells"*3) print_function() #Now write a function that takes one number as a parameter and returns the number doubled plus one as the output vnumber = int(input("Please choose a number")) def f_print_equation(vnumber): print(int(2*vnumber+1),

Lesson 6 Mars Bars experiment:

#Function def f_ask_yes_no(question): """Ask Yes or No Question""" vResponse = None while vResponse not in ("yes","no"): vResponse = input(question).lower() return vResponse #Main Program vQuestion = "Would you like a Mars Bar?" vAnswer = f_ask_yes_no(vQuestion) if vAnswer == "yes": print("Here is a Mars Bars, I hope you enjoy it") if vAnswer == "no": print("No Mars Bar for you :(")

Lesson 6 Mars Bars Result:

Menu Options Input:

def f_menu_options(): print("Here is the menu for today" "\n1. Appetizer" "\n2. Soup of the Day" "\n3. Main Course" "\n4. Rice or Noodles" "\n5. Dessert of the Day") vAppetizer = "Here is your appetizer" vSoup = "Here is your soup of the day" vMainCourse = "Here is your main course" vrice = "Here is your choice of rice or noodles" vdessert = "Here is your dessert of the day." vChoice = input(print("Please press 1 for appetizer, 2 for soup, 3 for main course, 4 for rice or noodles, 5 for dessert, or 6 for exiting the code")) if vChoice == 1: print(vAppetizer) if vChoice == 2: print(vSoup) if vChoice == 3: print(vMainCourse) if vChoice == 4: print(vrice) if vChoice == 5: print(vdessert) else: print("Thanks for coming, come back soon!") f_menu_options()

Menu Options Output:

In Python, functions enable you to reuse logic an infinite number of times without repeating yourself. In a way, functions are similar to variables and store information, but instead of just storing strings and values, functions can store loops, strings, equations, and values. Using functions can help you reuse the code easily without having to write out the entire code. Functions are written with the keyword **def** followed by the function name and parentheses, and then a colon, and these parentheses contain the parameters of the function. Once information is stored into the function, one can easily recall the function by using the function name. For example, in the lesson 5 input, I called the function by saying “f_print_largest (int1, int2)”.

In the Chocolate Machine Challenge, I used my knowledge of variables, simple math, and other functions to create a simple chocolate vending machine. With the given price of $16.9, I let the consumer input how much money they are going to pay, then using that value I find the change required to give, how much ten/five/one dollar bills and cents are required to pay back this change. The result/output is shown in the video.

For Lesson 6, we practiced using functions and defining variables, applying it to real world things like a menu. The menu project has not been finished yet, but that’s what we have done so far.