2017-02-08 HW

posted Feb 8, 2017, 6:55 AM by Samuel Konstantinovich   [ updated Feb 8, 2017, 8:29 AM ]
Goal: While loops
(Loops will not be on Friday's quiz)

Python while loops take the following format:
while boolean_expression:
    inside of loop   
    do code here
    #still inside of loop   
outside loop

Evaluate the boolean expression:
    When the boolean is true: Do the entire body, then check the boolean again.
    When the boolean is false: Skip the entire body, and continue with the rest of the code.

**See Examples below.


Classwork:  With a partner, open Idle, and write code to accomplish the following goals. Come up with tests to verify you are correct! BEFORE class ends, email the code to both you and your partner. You will switch who is typing halfway through.
    1. Write a loop that prints the sequence   2,4,6, ... 28,30.  (multiples of two from 2 to 30 inclusive)
    2. Write a loop that prints the multiples of 3 from 12 to 60 (inclusive).
    3. Write a loop that prints all of the perfect squares from 1 to 10,000 inclusive (the squares are between 1 and 10000, you are not squaring 10000!!!!)
   ...Give the keyboard to your partner!
    4. Write a loop that calculates the sum of all of the numbers between 0 and 3000 that are multiples 5. Print the sum.
    5.Write a loop that calculates the sum of all of the numbers between 0 and 3000 that are multiples of 5 and also multiples of 7. Print the sum.  

Homework:
I placed custom codingbat problems here:  (under set2 )

You have had regular codingbat problems to do, but also custom ones! (set1) In order to check custom problems, remember to go back to this link for custom problems.






print "### Example 1: ###"

counter = 10
while counter > 0:
    print counter
    counter = counter - 1


print "### Example 2: ###"

i = 1
while i < 500:
    print i
    i = i * 2


print "### Example 3: ###"

temp = 200.0
while temp > 150:
    print "cooling off: ",temp
    temp = temp * .95
print "cool enough at ", temp

PROGRAM OUTPUT: 
### Example 1: ###
10
9
8
7
6
5
4
3
2
1
### Example 2: ###
1
2
4
8
16
32
64
128
256
### Example 3: ###
cooling off:  200.0
cooling off:  190.0
cooling off:  180.5
cooling off:  171.475
cooling off:  162.90125
cooling off:  154.7561875
cool enough at  147.018378125

Comments