Presentation is loading. Please wait.

Presentation is loading. Please wait.

Writing Functions( ) (Part 4)

Similar presentations


Presentation on theme: "Writing Functions( ) (Part 4)"— Presentation transcript:

1 Writing Functions( ) (Part 4)
Loops, Random, Return

2 Practice Define a function that passes four arguments and calculates the final price of an investment/loan You never have to write this function again!! Then, ask the user for their initial amount, rate, number of times compounded a year and the number of years and use the function to calculate the final price

3 Returning Values Now that we’ve created a communication street with our functions, let’s bring it back around We can define our functions to send back information to us using the “return” keyword Remember, that the functions must then be called to a variable location!

4 Return Value def cube ( num1 ): cubed = num1 ** 3 return cubed number = int(input(“Give me a number:”)) cube(number) # this will not output anything because the returned value does not have a location nor was it printed

5 Return Value def cube ( num1 ): cubed = num1 ** 3 return cubed number = int(input(“Give me a number:”)) new = cube(number) print(new)

6 Return Value def cube ( num1 ): cubed = num1 ** 3 return cubed number = int(input(“Give me a number:”)) print(cube(number)) Note: you can return values to be printed right away

7 Practice Define a function that passes three arguments denoting three different test scores from the user and returns the average of the test scores Then, call the function with the given test scores of 96, 88 and 90 and print out the average

8 Practice Define a main function that asks the user for the test scores and passes them into the average function you created from the previous exercise Run this main function for two different users and compare values to see who got the higher grade Print ONLY the higher grade average and congratulate that user

9 Returning Multiple Values
As with passing arguments, we can define functions to return multiple values at a time. Again there is a warning, you must return all values into the appropriate number of variable locations Values will be returned to their respective located variables

10 Return Value def powers( number ): squared = number ** 2 cubed = number ** 3 fourth = number ** 4 return squared, cubed, fourth num1, num2 = powers(10) # this will cause an error because the function returns 3 values and is called into only two locations

11 Return Value def powers( number ): squared = number ** 2 cubed = number ** 3 fourth = number ** 4 return squared, cubed, fourth num1 = powers(10) # this will not cause an error but the three values will be returned to the variable num1 as a list

12 Return Value def powers( number ): squared = number ** 2 cubed = number ** 3 fourth = number ** 4 return squared, cubed, fourth num1, num2, num3 = powers(10)

13 IPO Notation Stands for Input, Processing, and Output notation.
Programmers use IPO to describe the type of function they need to create. When given IPO notation, you must follow the rules for the number of arguments expect (input), what it should do (processing) and the number of values returned (output)

14 IPO Notation IPO notation is usually written as a comment inside of source code Example: # Input: passes 2 arguments # Processing: adds the two numbers # Output: returns 1 value as a sum def function (a, b): return a + b

15 Practice Define a function that passes four arguments (in any order), alphabetizes them and returns all four names in alphabetical order into separate variables labeled: “first”, “second”, “third”, and “fourth” Call the function with the input function passed four times as each of the required arguments and have the names printed out in alphabetical order, and then in reverse order

16 Practice Define a function that:
Input: passes four arguments Processing: alphabetizes arguments, sorts from least to greatest Output: returns four values in order from least to greatest Call the function with the input function passed four times as each of the required arguments and have the names printed out in alphabetical order, and then in reverse order

17 Practice Now, combine the main average function and the alphabetizing function to see if we can organize which of the fours users achieved the highest grade Then, print out the scores from high to low

18 Random Numbers Python has the ability to generate random numbers
We can achieve this using the randint( ) function The randint( ) function takes two arguments as the inclusive boundaries from which to choose a value from, it then returns an integer value at random

19 Random Numbers However, randint( ) is not a function that is embedded into Python’s main shell Therefore, we need to import a file called “random”, then we can call the specific function from the file

20 Random Numbers

21 Practice Define a function that recreates the guessing magic number game but this time, make the magic number randomly generated Make sure the function includes statements for whether the guess was too high or too low


Download ppt "Writing Functions( ) (Part 4)"

Similar presentations


Ads by Google