lunch time
play

Lunch Time? Programming Construct Three: Repetition Repetition - PowerPoint PPT Presentation

17/06/2013 Lunch Time? Programming Construct Three: Repetition Repetition Statements While Loops Python supports two types of loop (repetition) statement: 1. For Loops, and 2. While Loops. 1 17/06/2013 The While Loop


  1. 17/06/2013 ¡ Lunch Time? Programming Construct Three: Repetition Repetition Statements While Loops • Python supports two types of loop (repetition) statement: 1. For Loops, and 2. While Loops. 1 ¡

  2. 17/06/2013 ¡ The While Loop Statement Problem Example 8: ASCII While <TEST> : Character Set <STATEMENTS> • General purpose loop for repeating some sequence of statements. ASCII Character Set Requirements ASCII Character Set Source Note that some ASCII Produce a Python • Load PythonExampleProblems\Repetition character codes are program that outputs the \ASCIIcharacterSet unprintable. For example: ASCII ( American \asciiCharacterSet.py into the IDLE editor. Standard Code for 7 = Beep Information counter = 0; 9 = Horizontal tab Interchange ) character 10 = Line feed set from 0 to 127 while counter<128 : 27 = Escape print('{0} = {1}'. \ (inclusive). 32 = Space format(counter,chr(counter))) counter += 1 127 = Delete 2 ¡

  3. 17/06/2013 ¡ Run The ASCII Character Set Source Code • Try printing out the first 96 (codes 0 to 95 Problem Example 9: Menu inclusive) characters. Input Menu Input Requirements Menu Input Source • Load PythonExampleProblems\Repetition Design and implement some Python code \MenuInput\menuInput.py into the IDLE editor. that allows the user to select from five • Note that it features an infinite loop (the test statement different menu options on a continuous loop, comprises the Boolean vale True which evaluates to including an option to quit the program. itself) and that the termination statement is embedded in the loop using an “if” statement. Include an error handling mechanism for the situation where an unrecognised menu while True : while True : option is input by the user. <STATEMENTS> <STATEMENTS> or if <TEST> : if <TEST> : quit() break 3 ¡

  4. 17/06/2013 ¡ Run The Menu Input Source Code For Loops The For Loop Statement for <TARGET> in <OBJECT>: Problem Example 10: <STATEMENTS> Landscape Gardening Material Costs Update • Typically used for processing lists and dictionaries. 4 ¡

  5. 17/06/2013 ¡ Landscape Gardening Material Cost Landscape Gardening Material Cost Update Requirements Update Source Code • AQA GCSE Specimen Controlled Assessment • Load PythonExampleProblems example, Task 3: \FileHandling\MaterialCostUpdate \materialCostDictionary.py into the “ Develop a way to allow the user of the system to IDLE text editor. store the costs of raw materials in an external file so that these costs can be changed when the price of the materials rise and fall. ” Landscape Gardening Material Landscape Gardening Material Cost Update Comments (1) Cost Update Comments (2) • Note how the for loop to assign values to the • Note how the for loop to output the material costs material costs dictionary is defined: dictionary is defined: index = 0 for key, value in materialCost.items() : for key in materialCost.keys() : print('{0} = {1}'.format(key,value)) materialCost[key] = \ float(newMaterialCosts[index]) • To obtain just the dictionary values: index=index+1 for value in <DICTIONARY>.values() : print(value) 5 ¡

  6. 17/06/2013 ¡ Run Landscape Gardening Material Cost Update Source Code • There is a material costs file that can be used: More Loop Examples newMaterialCosts.txt • Try editing this file and then loading it again. Monthly Quote Summarisation Report Requirements • AQA GCSE Specimen Controlled Assessment Problem Example 11: example, Task 4: Monthly Quote “ The Managing Director of the company has asked if there is a way that she can use this system to prepare a Summarisation Report monthly report that compares the total cost of all materials purchased per month across all jobs undertaken. The user of the system would type in the name of the month or the month number (e.g. 6 for June). The program will then read all of the materials purchased for that month and calculate the totals … ” 6 ¡

  7. 17/06/2013 ¡ Monthly Quote Summarisation Monthly Quote Summarisation Report Comments (1) Report Source Code • Assumes the existence of a quotesToDateFile.txt of the form: • Load PythonExampleProblems April quoteNumber1.txt \FileHandling\SummarisationReport April quoteNumber2.txt \summarisationReport.py into the May quoteNumber3.txt IDLE text editor. May quoteNumber4.txt • And a set of associated quote files of the form: May 3 7 325.5 420.0 0 0 0.0 0.0 1 150.0 60.0 Monthly Quote Summarisation Run The Quote Summarisation Report Comments (2) Report Source Code • Note the way that the quote file input loop operates: • By default six quote files are used, two for “April”, two for “May” and two for “June”: index = 0 while True : quoteNumber1.txt if (quotes[index]==month) : quoteNumber2.txt inputQuoteFromFile2(quotes[index+1]) <STATEMENTS> quoteNumber3.txt index = index+2 quoteNumber4.txt if (index>=len(quotes)) : break 7 ¡

  8. 17/06/2013 ¡ Landscape Gardening III Source Code Problem Example 12: • Load PythonExampleProblems \FileHandling Landscape Gardening \LandscapeGardeningIII III, The Final Cut! \landsGardQuoteIII.py into the IDLE text editor. Home Time? Run The Landscape Gardening III Source Code 8 ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend