120 likes | 176 Views
Python Programming. Using Variables and input. Objectives We’re learning to build functions and to use inputs and outputs. Outcomes Build a function Use inputs and outputs Introduce a parameter. Isn’t there another way? - Starter.
E N D
Python Programming Using Variables and input
Objectives • We’re learning to build functions and to use inputs and outputs. Outcomes • Build a function • Use inputs and outputs • Introduce a parameter
Isn’t there another way? - Starter Discuss alternative ways of doing this. This is a program which asks for a name input 5 times. print (“what is your name”) name = input() Print(name) print (“what is your name”) name = input() Print(name) print (“what is your name”) name = input() Print(name) print (“what is your name”) name = input() Print(name) print (“what is your name”) name = input() Print(name)
Isn’t their another way? – Starter / Answers FUNCTIONS!! def main(): print (“what is your name”) name = input() Print(name) In terms of programming, the function is something which carries a sequence of instructions. It saves us from repeating instructions over and over.
Don’t forget your input? In your previous lesson, you learnt how to store variables. We completed this program which asks for our name and age. If we want to re-use this we can put it in a function. Lets break this down….
What is a function? In your previous lesson, you learnt how to store variables. The first thing you must do is define the function. This lets the program know that the function is available to use. This function creates the text hello. You should open up a new program in python and set up your code so that it uses a function. Try running it by saving it as a .py file and then press F5 on your keyboard. When in the shell, type main() to see it work.
Tasks ? Write the following functions: • A function to save your name and print it to the screen. • A function to ask the user for 2 numbers and add them together • A function to ask the user for their name, age and place of birth and build a sentence with the use of the input variables.
Extension – Introduction of parameters? • When creating a function within your game, you can include parameters. • They are used to customise the program. • You don’t always need the program to ask for input. You can use parameters to pass in filenames, text and other variables.
Tasks ? Modify your functions to include parameters: • Include your name in a parameter of your first function. • Use two parameters to calculate numbers. • Use the previous example to create your own function which uses parameters for age, name and place of birth.
Plenary Self Assessment: • Your teacher has issued you with a self assessment sheet. You will have today’s objects and outcomes on it. Task • Comment on the lesson. Talk about the positive experience you have had and talk about things you need a little more help on. If you managed to get onto the extension tasks, what have you found out? Include the parameters in the correct place • Your teacher has given you a piece of code. You must include the correct parameters in the correct place.