2018-02-08 HW

posted Feb 8, 2018, 6:11 AM by Konstantinovich Samuel   [ updated Feb 8, 2018, 11:08 AM ]
Goal: While loops

(Exam Next Thursday. - All python up to loops)

Homework: (should be done over the next 2 nights)
Codingbat problems:  
Loop Numbers (some overlap the classwork)
Also complete Parrot_trouble (i forgot to assign it, but it is very easy)


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.



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


Classwork:  PAIR PROGRAMMING
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)
    ...Give the keyboard to your partner!

    2. Write a loop that prints the multiples of 3 from 12 to 60 (inclusive).
    ...Give the keyboard to your partner!

    3a. Write a loop that prints all of the perfect squares from 1 to 5,000 inclusive (the squares are between 1 and 5000, you are not squaring 5000!!!!)
        e.g. The squares from 1 to 30 are : 1 4 9 16 25
   ...Give the keyboard to your partner!

    3b. Write a loop that prints the first 104 perfect squares starting with 1. 
    ...Give the keyboard to your partner!
    
    4. Write a loop that calculates the sum of all of the numbers between 1 and 3000 (inclusive) that are multiples 5. Print the sum.
    ...Give the keyboard to your partner!
    
    5.Write a loop that calculates the sum of all of the numbers between 1 and 3000 (inclusive) that are either multiples of 5 or multiples of 7. Print the sum.  


Comments