More While Loops.

Slides:



Advertisements
Similar presentations
Order of operators: x ** y x * y, x / y, x // y, x % y x + y, x - y
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Def find_largest(x,y): largest = 0; count = 0; while count < x: rnum = randrange(0,y) if rnum > largest: largest = rnum count +=1 return(largest); print(find_largest(7,50))
Algorithm and Flowchart Questions
Flowcharts Remember that a solution to a problem is called an algorithm. Algorithms are often a series of steps required to solve the problem. A flowchart.
CHAPTER 2 ANALYSIS OF ALGORITHMS Part 2. 2 Running time of Basic operations Basic operations do not depend on the size of input, their running time is.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Python: Modularisation Damian Gordon. Modularisation Remember the prime checker program:
First Program  Open a file  In Shell  Type into the file: 3  You did it!!! You wrote your first instruction, or code, in python!
PPT 2. Other things: functions  Can you have a function with no inputs?  Yes: def f( ): return (3 + 4 )  Can you have a function with no outputs? 
Lecture Set 5 Control Structures Part D - Repetition with Loops.
More on Functions (Part 2) Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
PPT3. Quick function:  Write a function that checks to see if a number is even or not.  What TYPE does this return?
Basic Control Structures
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Intro to Nested Looping Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Strings:  Strings?  concatenate (join) +  make multiple copies using *  compare: > =
Largest of x random numbers def find_largest(x,y): largest = 0; count = 0; while count < x: rnum = randrange(0,y) if rnum > largest: largest = rnum count.
Loops ! We've seen variables change in-place before: [ x*6 for x in range(8) ] [ 0, 6, 12, 18, 24, 30, 36, 42 ] remember range ?
EECS 110: Lec 10: Definite Loops and User Input Aleksandar Kuzmanovic Northwestern University
Prime Numbers Damian Gordon. Prime Numbers So let’s say we want to express the following algorithm: – Read in a number and check if it’s a prime number.
Tutorial 9 Iteration. Reminder Assignment 8 is due Wednesday.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Arrays What is an array… –A data structure that holds a set of homogenous elements (of the same type) –Associate a set of numbers with a single variable.
First Program  Open a file  In Shell  Type into the file: 3  You did it!!! You wrote your first instruction, or code, in python!
More on Functions (Part 2) Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
LISTS: A NEW TYPE! We've got: ints floats booleans strings and now… (drum roll) LISTS! sets of a thing (e.g., sets of ints)
Scientific Programming in Python -- Cheat Sheet
EECS 110: Lec 10: Definite Loops and User Input
CSc 110, Autumn 2017 Lecture 13: Cumulative Sum and Boolean Logic
Introduction to Python
Python unit_4 review Tue/Wed, Dec 1-2
More on Functions (Part 2)
Repetition Structures
Prime and Composite.
Other things: functions
5. Function (2) and Exercises
While Loops in Python.
Lecture 07 More Repetition Richard Gesick.
Factors, multiple, primes: Factors from prime factors
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
Building Java Programs
Building Java Programs
EECS 110: Lec 10: Definite Loops and User Input
Intro to Nested Looping
Intro to Nested Looping
Repetition Structures
Adapted from slides by Marty Stepp and Stuart Reges
Python: Algorithms Damian Gordon.
Building Java Programs
What does this do? def revList(L): if len(L) < = 1: return L x = L[0] LR = revList(L[1:]) return LR + x.
Introduction to Programming Using Python PART 2
CSc 110, Spring 2018 Lecture 13: Cumulative Sum and Boolean Logic
More on Functions (Part 2)
More on Functions (Part 2)
Writing Functions( ) (Part 4)
Intro to Nested Looping
2/24/2019.
Building Java Programs
What is printed out? def f(x): print(x+3) return(x + 7) def g(k): return(f(k) + 2) print(g(1)) 4 10 def f2(x): return(x + 7) print(x+3) def g2(k): return(f2(k)
Strings: What can we do with Strings?
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Intro to Nested Looping
Strings: Strings? concatenate (join) + make multiple copies using *
Topic: Loops Loops Idea While Loop Introduction to ranges For Loop
Factors, multiple, primes: Multiples
While Loops in Python.
REPETITION Why Repetition?
Presentation transcript:

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)))