coffee time
play

Coffee Time? Programming Construct Two: Selection Selection - PowerPoint PPT Presentation

17/06/2013 Coffee Time? Programming Construct Two: Selection Selection Statements Problem Example 4: Python, unlike some other programming Triangles languages, only has one types of selection statement, the if-else statement.


  1. 17/06/2013 ¡ Coffee Time? Programming Construct Two: Selection Selection Statements Problem Example 4: • Python, unlike some other programming Triangles languages, only has one types of selection statement, the “if-else” statement. • Although Python support variations of this statement. 1 ¡

  2. 17/06/2013 ¡ Triangles Introduction Triangles Requirements • The anatomy of the Python “if-else” statement is • Produce a Python program which, given three as follows: sides of a triangle, determines whether the triangle is either: if (<BOOLEAN EXPRESSION>): <STATEMENTS X> 1. Equilateral (all sides the same if (<BOOLEAN EXPRESSION>): <STATEMENTS X> length), else : 2. Isosceles (two <STATEMENTS Y> sides the same if (<BOOLEAN EXPRESSION>): length), or <STATEMENTS X> elif : 3. Scalene (no sides <STATEMENTS Y> the same length). else : <STATEMENTS Z> Triangles Source Code Triangles Comments (1) • Lots of “if-else” statements! • Load PythonExampleProblems • Test part of “if-else” can be a Boolean operator \Selection\Triangles\triangles.py ( < , >= , == , <= , > ) or a Boolean value ( True , False ). into the IDLE text editor • We can concatenate Boolean operators and values using logical operators ( and , or , not ). • Note use of the global statement (only required when assigning values to “global variables”). • Test included to ensure user has defined a realisable triangle. 2 ¡

  3. 17/06/2013 ¡ Run The Triangles Source Code Problem Example 5: Test Case Expected result Side A Side B Side C Calculator 5 4 4 Isosceles 4 3 5 Scalene Test Cases 4 5 4 Isosceles Designed to 3 4 5 Scalene test every path 5 2 2 Error 4 4 4 Equilateral through 4 4 2 Isosceles programme 4 2 4 Isosceles Calculator Requirements Calculator Source Code • Develop a calculator Python program that can • Load PythonExampleProblems resolve simple arithmetic expressions of the form: <OPERAND> <OPERATOR> <OPERAND> \Selection\Calculator ¡ \calculator.py into the IDLE text editor. Where <OPERAND> is an integer of some kind and <OPERATOR> is one of the operators + , - , * or / (integer division). Calculator Comments • Thus given the expression 63*35 the program should calculate the value and display the result. • Note quit() function. • Remember to include a divide by zero test! ¡ 3 ¡

  4. 17/06/2013 ¡ Run The Calculator Source Code Test Case Expected Data Structures result operand1 operator operand2 3 + 2 5 3 - 2 1 Test Cases 3 * 2 6 3 / 0 Error 3 / 2 1 3 % 4 Error • Try adding the % operator! Data Structures • The most commonly used Python data structures Problem Example 6a: are: Distance Conversion 1. Lists 2. Dictionaries Version 1 (Lists) • (There are others, for example tuples) 4 ¡

  5. 17/06/2013 ¡ Distance Conversion Source Distance Conversion Requirements Code Version 1 Design and create a piece of Python software • Load PythonExampleProblems that, when presented with a distance given in \ ListsAndDictionaries Metres converts it to a distance measure \DistanceConversion comprising Yards, Feet and Inches (1 Metre \distanceConversionVer1.py into the = 39.37 Inches, 12 inches = 1 foot, 3 feet = 1 IDLE text editor. yard). Output the result in whole Yards, Feet and Inches. Run The Distance Conversion Source Distance Conversion Comments Code Version 1 • We declare an empty list using: <LIST_NAME> = [] . Test Expected Case result Test 0 [0,0,0] • We can add to a list using the append method: Cases 1 [1,0,3] append.<LIST_NAME>(<NEW_ITEM>) . 4 [4,1,1] 50 [54,2,0] ¡ • We can access individual elements by “indexing” 100 [109,1,0] in to the list: <LIST_NAME[<INDEX>] . 5 ¡

  6. 17/06/2013 ¡ Distance Conversion Source Code Version 2 Problem Example 6b: • Load PythonExampleProblems / Selection/ListsAndDictionaries/ Distance Conversion DistanceConversion/ Version 2 (Dictionaries) distanceConversionVer2.py into the IDLE text editor. Run The Distance Conversion Source Distance Conversion Comments Code Version 2 • We declare an empty dictionary using: <DICTIONARY_NAME> = {} Test Expected Case result Test • We can add to a list using the append method: 0 [0,0,0] Cases 1 [1,0,3] <DICTIONARY_NAME>[<LABEL>] = 4 [4,1,1] <VALUE> [54,2,0] ¡ 50 100 [109,1,0] • We can access individual elements by “indexing” in to the list: <DICTINARY_NAME[<LABEL>] 6 ¡

  7. 17/06/2013 ¡ Writing To Files • We “open” a file using the statement: <NAME> = open(<FILE_NAME>,<MODE>) Writing and Reading To and From Files Frequently used modes are ‘r’ (read only), ‘w’ (write only) and ‘a’ (append). • Write to an opened file as follows: <NAME>.write(<CONTENT>) • And close the file at the end using: <NAME>.close() Reading From Files Writing to Files Example • In a terminal window change directory to the • As before we “open” a file using the statement: directory PythonExampleProblems\Temp (in <NAME> = open(<FILE_NAME>,<MODE>) your directory structure). • Open the python interpreter and type the following: • Read from an opened file as follows: file = open('myfile.txt','w') <CONTENT>=<NAME>.read() print file file.write('I am really enjoying learning ') file.write('about Python today\n') • And close the file at the end as before: file.close() <NAME>.close() • Now open the file you have created in a text editor! 7 ¡

  8. 17/06/2013 ¡ Writing to Files Example (2) Writing to Files Example (1) • You should see something like: • Still in your Temp directory type the following in >>> file = open('myfile.txt','r') the Python interpreter: >>> text = file.read() >>> print text file = open('myfile.txt','r') I am really enjoying learning text = file.read() about Python today file.close() >>> text.split() print text ['I', 'am', 'really', 'enjoying', text.split() 'learning', 'about', 'Python', 'today'] >>> file.close() >>> • Exit the Python interpreter Landscape Gardening II Introduction Problem Example 7: • AQA GCSE Specimen Controlled Assessment Landscape Gardening II, example, Task 2: This Time With Dictionaries “ The Company has asked if it would be possible to and File Output! save customer quotations so that these can be viewed at a later date. Create a section of the program that allows quotations to be saved ... ” 8 ¡

  9. 17/06/2013 ¡ Landscape Gardening II Landscape Gardening II Source Comments (1) Code • Data now stored as dictionaries. • Load PythonExampleProblems MATERIAL_COST = {'Lawn' : 15.5, \FileHandling 'Patio' : 20.99, 'Water Feature' : \LandscapeGardeningII 150.0} \landsGardQuoteII.py into the IDLE text quote = {'Lawn' : {'Length' : 0, editor. 'Width' : 0, 'Cost' : 0.0, 'Time' : 0.0}, 'Patio' : {'Length' : 0, 'Width' : 0, 'Cost' : 0.0, 'Time' : 0.0}, 'Water Feature' : {'Quantity' : 0, 'Cost' : 0.0, 'Time' : 0.0}} Run The Landscape Gardening II Landscape Gardening II Source Code Comments (2) • Note use of nested dictionaries. • In your LandscapeGardeningII directory • We access items in nested dictionaries as follows: check the file you have created! quote['Lawn']['Length'] • Now we have ability to write a quote to file. 9 ¡

  10. 17/06/2013 ¡ Lunch Time? 10 ¡

Recommend


More recommend