t_08.4up - Engineering Computation: Tutorial 08 Problem...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Engineering Computation: Tutorial 08 Problem Solving with Computing, using the Python Language Questions from Week 07? Programming Exercises from Zelle textbook 1 pg. 262 - 263 # 2, 3, 5, 6, 12. 2 Whats in your toolbox? Print statement, string literals Basic calculator, integer division Variables Built-in functions Math library functions Non-branching programs Branching programs Python lists For loops Nested for loops Strings Converting between number system using Python Matplotlib Algorithms File Processing Graphics Functions Scope of Variables Comments Complex Conditions Testing and Debugging Exception Handling 3 Todays Topics: 1 While loops 2 Common Loop Patterns 3 Boolean Operators 4 Unit Testing 4 Loops Recall: the for loop for v a r i a b l e in l i s t : block The for loop iterates over a sequence. This particular loop pattern is called a counted loop . 5 Loops The for loop is useful if you know how many iterations of the loop you need. Counted loops can also be written using a while loop. There are situations where its impossible to know the number of iterations in advance. In such cases, a for loop can not be used. In such cases we can use a while loop ( conditional loop). 6 Loops while loops are sometimes called conditional loops because they iterate only as long as some condition is true. the general form of the while loop is: while condition : block 7 While Loops Here is a trivial example: >>> i = 0 >>> while i < 4: i = i + 1 print i, i 1 2 3 4 i < 4 true true true true false output 1 2 3 4 8 For Loops in More Depth For loops can always be transformed into while loops. [DEMO] for loop for index in range(1,5): print index, while loop index = 0 while index < 5: index = index +1 print index, 9 recall: Pyramid of Golf Balls In Tutorial 02 you built a 3 layer high pyramid with golf balls. You calculated how many golf balls you needed. 10 recall: Pyramid of Golf Balls Recipe You had to sum the number of golf balls in each level. 1 2 + 2 2 + 3 3 >>> pyramid = 1 ** 2 + 2 ** 2 + 3 ** 2 >>> pyramid 14 11 Exercise: Pyramid of Golf Balls What if, instead of trying to find out how many golf balls are in a pyramid of a given height, you were given a number of golf balls and where asked to find out how tall a pyramid you can build?...
View Full Document

This document was uploaded on 09/14/2011.

Page1 / 10

t_08.4up - Engineering Computation: Tutorial 08 Problem...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online