2014-03-04 + HW

posted Mar 4, 2014, 5:24 AM by Samuel Konstantinovich   [ updated Mar 4, 2014, 7:09 AM ]

Friday February 22, 2012

Goal: to use While loops



1.Write a description: how do we write a program to calculate the number of digits in an integer.

2.What do you need to be able to do in python to do this( what tools of programming)

3. What if you wanted to print something 13 times in python, how would you do it?



#Here is an example of python recursion: (so you know it actually works)

def countDigits(num):

   if num<10:

      return 1


      return 1 + countDigits(num/10)

What if you wanted to print something 13 times... some of you would copy paste.

Now what if you want to print something 1,000 times? Still want to copy paste?

Your goal today: Make a webpage with a table that has 1000 rows, each row has an integer and its square:

number square

1 1

2 4

3 9

... ...

1000 1000000

Do you still want to try to do this with copy/paste?

while boolean :

  code to repeat

  code to repeat


code outside of the loop

x=4 #this variable controls this loop

while x<10:

  print x

  x=x+1  #this makes x closer to the stopping condition

print "done" #this happens when the loop stops.

you can change the loop control variable (x in this case) by any amount you want. You can add 5 or subtract 2, depending on your goal. Your loop should get closer to ending every iteration(every time the loop is true and does something)


0. Wrie a loop that prints all of the values from 90 down to 0.

1. Write a loop that prints all of the multiples of 13 from 1 to 500.

        hint: You should print the values from 13 up to 494

2. Write a loop that calculates the sum of the perfect squares that are between  0 and 1000. Print the value. (1 + 4 + 9 ... + 

        hint 1: If you want to calculate a sum, you should store your answer in another variable such as:


        Then inside of your while loop you can say: 

        total = total + someValue

        hint 2: the sum is 10416, if you got a big number, you added the first 1000 perfect squres, instead of the perfect squares from 1 to 1000.

3. Write python code that prints a basic HTML table.

    Hint: (THIS IS NOT A TABLE! It is a hint) 

    print "<html>"

    print "<body>  blah blah </body>"

    print "</html>"  

    #is python code that prints a web page (not a fully compliant one)

3b. Write a loop around the part that prints the row, so that it repeats that row 15 times. Paste the result into an HTML file to see the table!

    Requirement (for the homework too): you should make sure your output looks like nicely formatted HTML:

GOOD: (appropriate new lines, good tabbing, you can use more new lines, but only if there are lots of td's in each row.)


   <tr> <td> blah </td>  <td> blah2 </td> </tr>

   <tr> <td> blah </td>  <td> blah2 </td> </tr>

   <tr> <td> blah </td>  <td> blah2 </td> </tr>


BAD:(all one line)

<table> <tr> <td> blah </td>  <td> blah2 </td> </tr>   <tr> <td> blah </td>  <td> blah2 </td> </tr>   <tr> <td> blah </td>  <td> blah2 </td> </tr> </table>

BAD2:(all new lines)








4. Challenge: write a loop that caculates the sum of the multiple of 3 or 5 from 1 to 1000. (this is the first problem on projectEuler.com ) You should check out projectEuler.com for challenging practice problems. They will help you force yourself to learn basic programming (and some math)

    Hint:  % is the remainder operator, so 15%11 → 4

HOMEWORK BigTable: *Submit on the homework server by 11:59PM  Wednesday, March 5th.

Submit a python program (just the py file) that makes a HTML table, size 2 columns x 1000 rows, filled with value , value^2. Remember to put your header on the file with your name etc.

Sample output:

<table border="1">

    <tr<td>1</td><td>1</td> </tr>
    <tr<td>2</td><td>4</td> </tr>
    <tr<td>3</td><td>9</td> </tr>
    lots more rows...
    <tr<td>1000</td><td>1000000</td> </tr>