Writing Functions( ) (Part 4)

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

Introduction to Computing Science and Programming I
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Computer Science 1620 Loops.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Main task -write me a program
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
Fundamentals of Python: From First Programs Through Data Structures
Fundamentals of Python: First Programs
An Introduction to Textual Programming
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
First Program  Open a file  In Shell  Type into the file: 3  You did it!!! You wrote your first instruction, or code, in python!
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Module 3 Fraser High School. Module 3 – Loops and Booleans import statements - random use the random.randint() function use while loop write conditional.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Coding Design Tools Rachel Gauci. What are Coding Design Tools? IPO charts (Input Process Output) Input- Make a list of what data is required (this generally.
1 Loops. 2 Topics The while Loop Program Versatility Sentinel Values and Priming Reads Checking User Input Using a while Loop Counter-Controlled (Definite)
30/10/ Iteration Loops Do While (condition is true) … Loop.
Logic Our programs will have to make decisions in terms of what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if.
Decision Structures, String Comparison, Nested Structures
Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)
Logical Operators, Boolean Variables, Random Numbers This template was just too good to let go in one day!
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Count Controlled Loops (Nested) Ain’t no sunshine when she’s gone …
First Program  Open a file  In Shell  Type into the file: 3  You did it!!! You wrote your first instruction, or code, in python!
COMPUTER PROGRAMMING Year 9 – lesson 1. Objective and Outcome Teaching Objective We are going to look at how to construct a computer program. We will.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
12.1 Rational Exponents 12.2 Simplifying Radicals.
Chapter 9: Value-Returning Functions
Introduction to Computing Science and Programming I
Recap: If, elif, else If <True condition>:
Repetition Structures
Introduction to Python
Line Continuation, Output Formatting, and Decision Structures
Formatting Output.
Control Structures II Chapter 3
Formatting Output.
Lecture 07 More Repetition Richard Gesick.
Lecture 4B More Repetition Richard Gesick
Review.
Random numbers Taken from notes by Dr. Neil Moore
Writing Functions( ) (Part 5)
Writing Functions( ) (Part 5)
The relational operators
Line Continuation, Output Formatting, and Decision Structures
© Akhilesh Bajaj, All rights reserved.
Exception Handling.
Pick a number, any number …
Decision Structures, String Comparison, Nested Structures
More Loops.
Count Controlled Loops
Coding Concepts (Sub- Programs)
Passing Parameters by value
We’re moving on to more recap from other programming languages
Do While (condition is true) … Loop
Repetition Structures
Writing Functions( ) (Part 4)
Computing Fundamentals
GUI Test Information.
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Introduction to Value-Returning Functions: Generating Random Numbers
Loops and Simple Functions
COMPUTER PROGRAMMING SKILLS
Loops.
Chapter 7 Empowering Programs with Math
Game Description Player 1: Decides on integer x > 0
Functions, Procedures, and Abstraction
Presentation transcript:

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

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

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!

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

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

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

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

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

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

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

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

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

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)

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

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

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

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

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

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

Random Numbers

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