Part One: Let the fun...ctions beginFunctions are blocks of organised code that can be reused to perform single or repeated actions. Python provides you with built-in functions such as print but you can make your own to have greater control over your code.
Task 1a: Create a flowchart that replicates a simple calculation programme. Check the steps below for details:
a) Ask a user for two numbers b) Add the two numbers together c) Output the result Task 1b: Now use Python to write this programme |
Part Two: Lets have some fun...ctionsThe flowchart for a while loop should show that the conditional code is happening within the loop. Take a look at figure 1. The loop will execute the conditional code until a condition is met. The loop then breaks and the programme continues (or not as the case may be!).
Task 2a: Watch the video and start to use functions to manage your code. Task 2b: Develop your code so that it uses two functions. Stuck? Use the steps below as a guide: a) call the new function add b) pass num1 and num2 into your add function c) move the result line into the function d) return result e) call both nums and add functions, check your syntax here f) print the result Really stuck? Are you seriously so stuck you need the full answer? By clicking here you agree that you have failed! |
|
Part Three: The fun...ctions never stopNow that you have a basic example of two functions save your work. This will act as an invaluable tool for future reference. Things can become complicated quite quickly.
Task 3: Add three more functions to your code so that you now have a (basic but...) fully working calculator. You'll also need to add some selection statements so that your programme knows which function to call. For example, a user will enter two numbers and then state which operator they want to apply (If choice=1 then call add). More ideas: 1. Add a loop to your code so that the main menu is presented after each calculation. This should only quit when the user hits "q" 2. Use functions to control a turtle that draws a variety of shapes by calling the functions repeatedly |