Presentation is loading. Please wait.

Presentation is loading. Please wait.

Count Controlled Loops (Nested) Ain’t no sunshine when she’s gone …

Similar presentations


Presentation on theme: "Count Controlled Loops (Nested) Ain’t no sunshine when she’s gone …"— Presentation transcript:

1 Count Controlled Loops (Nested) Ain’t no sunshine when she’s gone …

2 Practice – Divisibility  Write a program that asks the user for an integer  Then print out all numbers that are divisible by that number from 1 to 1,000

3 Practice – Divisibility (extension)  Extend your divisibility program to check for all integers from 1 to 10,000 that are divisible by two different integers simultaneously  Print out 10 numbers per line

4 Practice – Divisibility (extension)

5 Warm Up Exercise – Prime Numbers  Write a program that asks the user to enter an integers  Check to see if the number is prime and if not, print out the number that it is divisible by

6 Warm Up Exercise – Black Friday  Write a program that continually asks the user for a price value  When the user enters the word “end” you should end the program and add the total price of all the products  In addition, display the highest priced item and the lowest priced item

7 Lists  Python has such things called “lists”  Lists are denoted by brackets [ a, b, c, d ] and each item in the list is separated by commas  Lists can be stored and named as variables Example: x = [a, b, c, d]

8 Lists  One important thing to note about lists is that they can hold various data types all at once Example: list = [“name”, “word”, 1, 2, 3]

9 Nested Loops  A nested loop can be described as “a loop inside of a loop”  It’s the same idea as the nested “if” statements

10 Nested Loops  The inner loop of a nested loop will run through all of it’s iterations during each iteration of the outer loop  In other words, during one iteration of the outside loop, the inner loop will run it’s entire course (all iterations of the inner loop)  In order to find out the number of total iterations, multiply the number of iterations of all the loops

11 Practice - Times Table  Write a program that prints out the times table for the number 5 5 times 1 is 5 5 times 2 is 10 5 times 3 is 15 … 5 times 10 is 50

12 Practice - Times Table (Extended)  Write a program that prints out the times table for a range of numbers (user input) Range of numbers for times table (low): 2 High end of range: 4 2 times 1 is 2 3 times 1 is 3 4 times 1 is 4 2 times 2 is 4 3 times 2 is 6 4 times 2 is 8 2 times 3 is 6 3 times 3 is 9 4 times 3 is 12 … 2 times 10 is 20 3 times 10 is 30 4 times 10 is 40

13 Practice – It’s Time!  Write a program that prints out every single possible time value for the day, down to the second.  Output: 00:00:00 (12:00:00 am) 00:00:01 00:00:02 … 23:59:59 (11:59:59 pm)

14 Practice – Genesis Gradebook  Write a program that allows Mr. Seok to calculate the grade of each student in his class  First ask the user how many students are in his class  Then, for each student, ask the user for the score on each of three exams and calculate the average for each student  Then, print out the grades for each student

15 Practice – Genesis Gradebook How many students in your class? 6 For student 1: Test score 1: Test score 2: Test score 3: (repeat 6 times)

16 Practice – Genesis Gradebook Averages for Mr. Seok’s class Student 1: 95.4 % Student 2: 93.2 % Student 3: 88.6 % Student 4: 87.1 % Student 5: 80.0 Student 6: 76. 4%

17 Practice – Drawing  Write a program that outputs this image by using nested loops #

18 Practice – Pascal’s Triangle?  Write a program that reproduces this triangle of numbers by the use of nested loops 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6

19 Practice – Prime Numbers  Write a program that finds and prints out all the prime number from 1 to 1000.  Format your output so that only 5 numbers will print per line. They should align in columns.


Download ppt "Count Controlled Loops (Nested) Ain’t no sunshine when she’s gone …"

Similar presentations


Ads by Google