上降充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY When else meet for else execute when the for terminated "normally' ·Example: def find elem(mylist,elem): for i in mylist: if i==elem: print "find "elem break else: print "Not find "elem 11
When else meet for • else execute when the for terminated “normally” • Example: def find_elem(mylist, elem): for i in mylist: if i==elem: print "find ", elem break else: print "Not find ", elem 11
上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Indefinite Loops for loop got the job done,but you need to know ahead of time how many numbers you'll be dealing with. What we need is a way for the computer to take care of counting how many numbers there are. The for loop is a definite loop,meaning that the number of iterations is determined when the loop starts. 。Ve need another tool! 12
12 Indefinite Loops • for loop got the job done, but you need to know ahead of time how many numbers you‟ll be dealing with. • What we need is a way for the computer to take care of counting how many numbers there are. • The for loop is a definite loop, meaning that the number of iterations is determined when the loop starts. • We need another tool!
上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Indefinite Loops while <condition>: <body> The indefinite or conditional loop keeps iterating until certain conditions are met. condition is a Boolean expression,just like in if statements.The body is a sequence of one or more statements. Semantically,the body of the loop executes repeatedly as long as the condition remains true. When the condition is false,the loop terminates.3
13 Indefinite Loops • while <condition>: <body> • The indefinite or conditional loop keeps iterating until certain conditions are met. • condition is a Boolean expression, just like in if statements. The body is a sequence of one or more statements. • Semantically, the body of the loop executes repeatedly as long as the condition remains true. When the condition is false, the loop terminates
上游充通大¥ ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Indefinite Loops i=0 <oonditior心? while i <10: 光8 print i <body> i=i+1 for i in range(11): print i The condition is tested at the top of the loop. This is known as a pre-test loop.If the condition is initially false,the loop body will not execute at all. 14
14 Indefinite Loops • The condition is tested at the top of the loop. This is known as a pre-test loop. If the condition is initially false, the loop body will not execute at all. i = 0 while i <= 10: print i i = i + 1 for i in range(11): print i
上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Indefinite Loop What should you do if you're caught in an infinite loop? First,try pressing control-c -Close the window If that doesn't work,try control-alt-delete (Windows...) If that doesn't work,push the reset button! 15
15 Indefinite Loop • What should you do if you‟re caught in an infinite loop? – First, try pressing control-c – Close the window – If that doesn‟t work, try control-alt-delete (Windows…) – If that doesn‟t work, push the reset button!