More Flow Control
break, continue
WHEN LIFE GIVES YOU
Sue Inn Chng
Copyright By PowCoder代写 加微信 powcoder
WHAT DO YOU DO?
The University of 1
Flow of Execution – What we know so far?
– Runssequentiallyfromtopto bottom except in cases:
– Functions
• Functionbodyonlyexecutes
during function calls.
– Conditionals
• Executionpathdependson whether condition is True or False.
• Actions are repeated depending on whether condition is True.
The University of 2
Flow of Execution – What ifs?
– Wewanttocontinuealoopbutstopforawhiletotakeabreak?
– Iwantanearlyexitfromafunction,canIreturnnow?
– Iknowsomethingcangowrongduringexecutionwhenusageisdifferent from design. Should I raise this issue with my boss?
The University of 3
– Escape a loop early: – break
– continue
– Functionsre-visit:multipleexits/multiplevaluesuponexit
– Stopping execution – raise
– Followupexceptionhandling:try-except-finallyblocks.
The University of 4
– Aracerequiresparticipantstorun around the track FIVE(5) times. That’s you at the starting line!
– ThisisthePythonprogramthatyour actions are based on:
– Drawaflowchartofyouractions. The University of
Image credit: Clipartmax – Race Clipart Flag Png Page 5
Loops IRL: Ideal Case
Image credit: Clipartmax – Race Clipart Flag Png The University of 6
Run basic.py to view the output.
Loops IRL: When accidents happen…
– Whatwouldyoudoduringtherace if you:
– Fellandscrapeyourknee? – Fellandbrokeyourleg?
– Decisionmakingalgorithm: – Ifhavefallen,assessinjury.
• Broken leg, stop race.
• Scrape knee, continue race.
• No injuries only fall-prone, race as normal.
– Whereshouldthis“assessinjury algorithm” be placed in the flowchart?
The University of 7
Your task:
• Trace the pathway to work out the output for every round during the race.
• How do you get a
Perfect round.
on all rounds?
• If you have fallen and broken your leg, what
would be program’s output?
• Try: Race again: Random luck
The University of 8
Nutshell: break and continue
Immediate exit from the current loop. Remaining statements in the loop are ignored.
Skip the rest of this iteration and go on to the next one.
Body of loops
Body of loops
The University of 9
Reading This Week
– Pythondocumentation:
– https://docs.python.org/3/reference/simple_stmts.html#the-break-
– https://docs.python.org/3/reference/simple_stmts.html#the-continue- statement
– Chapter7.break.Downey,A.B.(2015).ThinkPython:Howto Think Like a Computer Scientist (2e ed.). O’ , Incorporated.
The University of 10
– Specialreservedkeyword.
– Whenusedinloops,itmeans ‘break’ from the current iteration of this loop.
– Itisonlyallowedinsidethe body of a loop.
The University of 11
Practice: break
– What is the value of distance at the
end of the program?
The University of Sydney – Implement the flowchart: Ed Lesson Page 12
distance+10
distance>72.5
– Anotherreservedkeyword.
– Whenusedinloops,itmeans ‘skip the rest of this iteration and go on to the next one’
– Itisonlyallowedinsidethe body of a loop.
The University of 13
Practice: continue –
What are the values ofxandyat the end of the program?
Implement the flowchart: Ed Lesson
The University of 14
Practice: break and continue
– Whatarethevaluesof variables x and y at the end of the program?
– Identify the output of the program.
– Whatwouldtheprogram output if the increment of x
is move just after line 5 before line 6?
The University of 15
Practice: break and continue
The University of 16
Practice: Nested loops with break
– A cashier can only serve 5 customers on each shift. During each shift, the cashier must check-out every item in the customer’s shopping basket. Write a program to track the items bought by each customer.
– If the cashier enters ‘end’, it denotes that there are no more items left in the current customer’s shopping basket. A customer can check-out any number of items.
– ProgramDesignthinking:
• How many loops do you need? What are their types?
• For each loop, what are the initial and termination conditions? • What are the actions to be do be done in each loop?
The University of 17
Practice: Nested loops with break
– Identify the count-controlled loop and
event-controlled loop.
– Wouldthebreakstatementexitthe outer loop?
– What are the data types stored in these lists:
– items_bought? – customer?
The University of 18
Practice: Customers Again
– Cashiers are intentionally serving ‘ghost’ customers, customers who has empty shopping baskets, to finish their shift quota. Modify the previous program to exclude ‘ghost’ customers.
– If there are customers who has empty shopping baskets, these customers are skipped. The program should not include these customers as a served customer.
– FlowchartgivenonEdLessonifyouarestuck.
The University of 19
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com