More While Loops
def func(x): def func(x): while (x > 0): stringvar = "" Write a function that takes as an input parameter an integer and uses a while loop to print out "ha" that number of times. Starting condition? What makes the loop stop? (your True/False condition?) What inside the loop changes so that the loop will stop? def func(x): while (x > 0): print("ha ",end= " ") x -= 1 return func(5) def func(x): stringvar = "" while (x > 0): stringvar += "ha ") x -= 1 return(stringvar) print(func(5))
Remember this? How do we modify it so that it: runs k times automatically keeps track of the number of times the numbers are equal? from random import * def jackpot(): x = randrange(1,10) y = randrange(1,10) print("x is "+str(x)+", y is "+str(y)) return (x==y) print(jackpot()) A) def jackpot(k): count = 0 while count < k: x = randrange(1,10) y = randrange(1,10) print("x is "+str(x)+", y is "+str(y)) count += 1 return (x==y) #THIS LINE MUST CHANGE! print(jackpot()) B) def jackpot(k): count = 0 tot = 0 while count < k: x = randrange(1,10) y = randrange(1,10) print("x is "+str(x)+", y is "+str(y)) count += 1 if (x==y): tot += 1 return tot print(jackpot())
What does this give you? def func(k): old = 0 new = 1 print(old,end= ", ") print(new, end = ", ") while old < k: current = old + new print(current, end = ", ") old = new new =current return print(func(100))
Let’s try: Write a function with a while loop that prints every other number starting at 1 and ending at 10 Write a function with a while loop that creates a string of every other number, with the loop starting at 1 and ending at 10, and returns that string
Problem 1:Write a function with a while loop that prints out every other number starting at 1 and ending at 10 Or: def g(k): x= 1 while (x <k): print(x, end = “ “) x = x + 2; return() print(g(10)) def g(): x= 1 while (x <10): print(x, end = “ “) x = x + 2; return() print(g())
Problem 2: Write a function with a while loop that creates a string of every other number starting at 1 and ending at 10 and returns that string Or def g(k): x= 1 y = “ “ while (x <k): y = y + str(x) x = x + 2; return(y) print(g(10)) def g(): x= 1 y = “ “ while (x <10): y = y + str(x) x = x + 2; return(y) print(g())
Let’s try: Write a function with a while loop that sums every other number between two integers that are input parameters to the function(you can assume the second integer is greater than the first integer) and returns that sum Write a function with a while loop that counts the number of numbers that is evenly divisible by 3 between the input parameters x and y
Problem 3: Write a function with a while loop that sums every other number between two integers def g(x,y): total = 0 while (x < y): total = total + x x = x + 2 return total print(g(3,12))
Problem 4: Write a function that counts the number of numbers that is evenly divisible by 3 between x and y Or: def h(x,y): total = 0 while (x < y): if x%3 == 0: total = total + 1 x = x + 3 else: x = x + 1 return total print(h(1,28)) def h(x,y): total = 0 while (x < y): if x%3 == 0: total = total + 1 x = x + 1 return total print(h(1,28))
Let’s try: Write a function with a loop that calculates x to the yth power, assuming we’ve only got multiplication (i.e., you can’t use **) Write a function with a while loop that determines whether a number is prime or not (and returns True if it is, and False if it isn’t) (Challenging!)
Problem 1: Write a function with a while loop that finds x to the yth power, assuming we’ve only got multiplication (i.e., you can’t use **) Could I do this? def k(x,y): while y > 0: x *= x y -=1 return x print(k(3,4)) print(k(2,4)) def k(x,y): total = 1 while y > 0: total *= x y -=1 return total print(k(3,4)) print(k(2,4))
2.Write a function with a while loop that determines whether a number is prime or not (and returns True if it is, and False if it isn’t) def f(y): ct = 2 while ct < y: if (y%ct==0): return False ct+=1 return True print(f(6)) print(f(137)) print(f(55)) print(f(29)) def f(y): ct = 2 isPrime = True while (isPrime == True && ct < y): isPrime = (y%ct==0) ct += 1 return isPrime print(f(6,2)) print(f(137,2)) print(f(55,2)) print(f(29,2))
Try? def nums(x,y): tot = 0 while y > 0: tot += x//y #Remember floor division? x = x%y y = y//10 return tot print(nums(1354,1000)) print(nums(254,100)) What did we just do?
Challenge: def sta(x): k = 0; while k < x: print(".",end = "") k +=1 print(":") return(x -1) sta(sta(sta(4)))