Presentation is loading. Please wait.

Presentation is loading. Please wait.

Def f(x): counter = 0 y = 0; while counter <=x: y += x counter += 1 return(y) assertEqual(f(4),______) What does this do? L OOPS :

Similar presentations


Presentation on theme: "Def f(x): counter = 0 y = 0; while counter <=x: y += x counter += 1 return(y) assertEqual(f(4),______) What does this do? L OOPS :"— Presentation transcript:

1 def f(x): counter = 0 y = 0; while counter <=x: y += x counter += 1 return(y) assertEqual(f(4),______) What does this do? L OOPS :

2 W HILE LOOPS def f(): count = input("Enter a number") total = 0 while count >= 1: total += count count = count -1 return(total) input: asks the user for some form of input – the user should type something in. Whatever the user types in is returned from input. In this case, it is put in the variable count.

3 def ThreeYearOld(): response = "" while response != "Because.": response = input("Why?") print("Oh. Okay.") ThreeYearOld() W HILE L OOP

4 W HILE LOOPS def f(): total = 0 while count >= 1: total += count count = count -1 return(total) What does this calculate?

5 W HILE LOOPS def f(): total = 0 count = 4; while count >= 1: total += count return(total) What does this calculate?

6 RULES : If using an iterator, it must be initialized before we start the loop (outside the loop!) count = 4; while count >= 1:... The loop has to stop. Inside the loop something must change each time so that we hit the stopping condition Like recursion, we must progress toward the stopping condition while count >= 1: total += count count = count -1

7 def f(x): return(x>5) def y(ls): ct = 0 while ((ct < len(ls)) and not(f(ls[ct]))): ct += 1 return(ct) list1 = [3,-1,2,4,8,7,5] assertEqual(y(list1),______)

8 def z(ls): ct = 0 x = 0 while (ct < len(ls)): if ls[ct] > ls[x]: x = ct ct += 1 return(x) list1 = [3,-1,2,4,8,7,5] assertEqual(z(list1),______)

9 def z(ct): x = 0 while (ct >= 0): x += ct ct = ct - 1 return(x) assertEqual(z(5),______) Versus: def z(ct): x = 0 while (ct >= 0): ct = ct - 1 x += ct return(x) assertEqual(z(5),______)

10 def q(ls): ct = 0 while (ct < len(ls)/2): ls[len(ls)-(ct+1)],ls[ct] = ls[ct],ls[len(ls)-(ct + 1)] ct += 1 return(ls) list1 = [3,-1,2,4,8,7,5] assertEqual(q(list1),________________)

11 def q(x,y): ct = 0 while (ct < x): ct2 = 0 while (ct2 < y): print ct + ct2 ct2 += 1 ct += 1 q(4,3)

12 def q(x,y): ct = 0 while (ct < x): ct2 = ct while (ct2 < y): print ct + ct2 ct2 += 1 ct += 1 q(4,3)

13 def q(x): ct = 0 while (ct < x): ct2 = x while (ct2 > ct): print ct + ct2 ct2 -= 1 ct += 1 q(4)


Download ppt "Def f(x): counter = 0 y = 0; while counter <=x: y += x counter += 1 return(y) assertEqual(f(4),______) What does this do? L OOPS :"

Similar presentations


Ads by Google