Extra Credit Opportunities

Some of you may feel you are beyond the tutorials and want to just try some new stuff. If this is you (and you have ask me if you think so), you can try some of the options below as replacement for the lessons or just for extra credit or plain ol’ fun.

Here are some ideas:

  • Fix the 1 cent problem to show your real (you get paid each day, not once a month) monthly/yearly pay.
  • Solve Euler #4 and find palindromes (this one is fun).
  • Find the 10001st prime number or a number with more than 500 factors (can you believe it?).
  • Use turtle and draw shapes.

Drawing Made with Turtle

Code to Draw the Star

Code to Draw the Star

Final Project

This is due no later than May 23rd. It is a 50 point assignment so make sure you finish it on time.

Assignment: create a “guess my number” game

There are two ways to approach this: you guess the computer’s number or the computer guesses your number. The second approach is a bit harder but would be a perfect challenge for the future high-paid coders in the class. Give it a try.

You need to know a couple things. First, if you want the computer to pick a number you need to know how to do random numbers. It’s pretty easy.

import random
number = random.randrange(50) + 1

You want to add 1 to the guess because the range starts at 0 and goes to 49 in this case. Adding 1 makes the range 1-50 which is what most people would expect. You can pick as big or small of a range as you want.

In fact, it would be cool to the let the player choose the range. To do that you would use the “raw_input” statement and then set your range to whatever the user input. For example,

lower_number = raw_input("Choose a number for the low end of the range")
upper_number = raw_input("Choose a number for the low end of the range")
guess = random.randrange(int(lower_number), int(upper_number) + 1)

Again, you add 1 to the upper end because the range function always goes to one less than the upper number. Also, raw_input returns a string and you need an integer so use int() to convert raw input to a number.

You will probably want to use a while loop for the game itself.

while guess != number:
    guess = int(raw_input("Enter a guess between 1 and 50"))

That just says that “while the guess is not equal to the number” do whatever (which will probably be your if statements to check if they guessed it).

if guess < number:
    (do something)
if guess > number:
    (do something)
if guess == number
    break #tells the while loop to exit because the player guessed the number

When you finish, you can build the game in Scratch for extra credit if you wish (meaning it can replace a missing assignment).

The $.01 Job

This is your next assignment. It is due Friday, May 10th. Keep working through the lessons as well. I will start to check off the ones you finish. Each of the first 5 assignmets, as well as this one, is a grade. All 6 need to be finished by May 10th or will be entered as a zero. You can turn in missing work without penalty until May 23rd. The first 5 Codecademy lessons are also a grade.

Python can be very useful for figuring out complex problems that would require a lot of pencil and paper work otherwise. Consider this odd paying job:

You get one cent on the first of the month and every day after your pay doubles until the last day of the month (so 2 cents on day 2 and 4 cents on day 3…). Your pay starts over again on the first of the next month. How much would you get paid each month (note that months have different numbers of days)? What is your total pay for the year?

To start, you should recognize that there are 3 lengths for months (not counting leap years) – 28 days, 30 days, and 31 days. So create three variables each with a starting pay of $0.01:

month28pay = .01
month30pay = .01
month31pay = .01

Next, you will need to double the pay for each option. For example, for February, which has 28 days normally, you start doubling on day 2 so you would double 27 times by the end of the month:

for i in range(27):
    month28pay = month28pay * 2

At the end of each loop (30 day months would double 29 times, etc.) your pay variable will equal the pay after doubling for that many days. All you need to do now is print out the pay for each month and add up the total for the 12 months. Give it a go. They are many different ways to do this. Feel free to be creative.

If you do it correctly you will get a total yearly pay of $97,978,941.44. That’s a job I’d take.

Your output would look similar to this:

January pay is $ 10737418.24
February pay is $ 1342177.28
March pay is $ 10737418.24

NOTE: the really smart ones in the class will recognize that this total is just your pay on last day of the month. You, of course, were paid $.01 on day one and $.02 on day two and a whole lot on the last couple days. Therefore, your January pay isn’t just the pay on the last day but also the pay for each of the other 30 days that month. Can you figure out how to add up your pay for EACH day each month? Bonus points available for anyone who does 🙂

Euler 13 Tips

In order to solve these and other assignments it is important that you continue to make progress through the Codecademy.com lessons.

If you want to try a fairly easy Euler Problem  give #13 a try – Large Sum

Knowledge of a couple tricks in Python makes this problem surprisingly easy. The first is “lists” which are covered in lesson 5 on Codecademy. To make a list, define a variable using brackets [] and separate elements with a comma.

For example:

numbers = [37107287533902102798797998220837590246510135740250,
46376937677490009712648124896970078050417018260538,
74324986199524741059474233309513058123726617309629,
91942213363574161572522430563301811072406154908250]

You can add up elements in a list using sum() like this:

large_sum = sum(numbers)

Of course, the problem asks you to print out just the first 10 digits which makes it a bit harder. Python sees a big number as just one thing. To allow Python to see each digit you make it a string using str(). Either make a new variable such as “large_sum_string” or add it to the line above. Then you can loop through the items in the string using a “for” loop to print out each one. Loops start at 0 unless you say otherwise so range(10) would go 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 and the first digit in the long string would also start at zero.

for i in range(10):
    print ...

In order to print an item in a string you put the number of the items place (starting from 0) in brackets like:

print large_sum[3]

That will print out the 4th digit in the large_sum string of numbers. You can also use a variable like “i” in the brackets like in a “for” loop. The loop approach works but prints each digit on its own line. There is a way to print it out on one line. Can you discover it??? (for extra credit).

Codecademy.com Issues

Well, it seems this site doesn’t work from school. Not sure why. The IT department is looking into it. It does work from outside the school’s network so I would encourage you to work on the lessons at home. Things are going to get confusing fast if you don’t find some time to make progress. You may also have some success using Safari at school. Give it a try.

In the meantime, I did find another site -> http://www.learnpython.org/. The lessons are not as good, but it is a good chance to explore various aspects of Python in class. Google also has a nice video lecture which some of you who want to delve more deeply might enjoy. Go to https://developers.google.com/edu/python/ and watch the “Lecture Videos day 1, day 2”. Another online tutorial you might try is http://www.trypython.org/.

Until we get the Codecademy issue worked out, you are going to have to be a bit more independent and focused. Below is a menu of items to try. Pick one and then explore a solution. Feel free to ask your peers or teachers for help but be sure to just try first. You’ll be surprised to find out how much you can learn if you just try.

Assignment: You will need to complete Two of these by May 3. They are graded.

Math Projects – These will give you a chance to practice doing basic math in Python

  1. Write a program that takes 2 numbers and prints out their sum, difference, product and quotient on separate lines like this…
    a = 45
    b = 9
    sum = 54
    difference = 36
    product = 405
    quotient = 5 (note: decimals are weird in Python. Use 45.0 or 9.0 to get decimal answers)
  2. More challenging, write a program to find out if a number is prime. Remember that the mod command (%) will return the remainder which if equals 0 (if i % x == 0) then the number can be evenly divided and is not prime. You would need to use a loop to check all possible numbers.

Interactive Projects – These will use the raw_input() command to ask a user to respond to something

  1. Write a program for #1 above that will let the user pick any two numbers. Note that if a user pics 0 it could give an error when dividing so you’ll need to check for that. You could also do the same for the prime number checker problem.
  2. Write a “game” that asks the user to pick among several options with a different outcome for each choice. If you want to add “dice” and random numbers you will need to explore the “import” command.

Euler Problems

  1. Work on solving any of the Euler problems with the exception of 1 (multiples of 3 or 5) or 6 (square sum difference). If you solved one in Scratch, you could redo it in Python. Euler 2 would be fairly easy.

How Smart is Python?

Powers of two are fun to play with. 2^10 is 1,024 and Python can calculate this very fast.

2^100 is a lot bigger. Here is a fun explanation of just how big.

Euler Problem 16 asks you to add up the digits in 2^1,000. It’s 302 digits long. Not too hard. You can do this problem after you learn about for loops and str().

Now you can make Python really work. 2^1,000,000 is really big. It’s 301,030 digits long. Python can print this number but good luck making sense of it. It can also add up the digits. Python is pretty smart.

Now for some fun…

2^57,885,161 – 1 is the largest prime number yet found and over 17 million digits long. Can you find the next one?

Python Assignments

Today we started learning how to code Python using the Codecademy.org website. The lessons are self-paced. You are free to work through them at your own pace. However, be sure to work steady as there are nine lessons to cover and each lesson consists of a number of exercises.

The second part of each lesson is a project. For example, the first lesson is “Python Syntax” and the project is the “Tip Calculator”. After you complete each project in each lesson you will also create your own program to do something similar but different.

For example, after completing the “tip calculator” project you could write a program to find 10% off any price (such as a coupon for a store or restaurant). Your example Python script might look like this…

# Calculate percent off a price
item_price = 44.99
item_price = item_price * .90
print "After your 10% discount, the new price is",("%.2f" % item_price)

You can write these in the Codecademy Labs editor or in Sublime or in text edit, etc. We will learn how to run these programs in an upcoming lesson. Each mini project like this is a graded assignment.

Python Editors

When we start Python it will be useful to have an editor for typing your code. There are some cool online editors but they all have some limitations like not being able to save your code for editing. If you want to work on Python at home it might be good to download one or two and start using them.

The two that I’ve found so far that seem pretty good are:

Both are free to try but eventually they want money 😦 Sublime however seems to let you keep working :).

We will use Pycharm in the labs in a few weeks but Sublime is maybe the easier one to start with. These work on both Mac and Windows but if you have a Windows computer you will need to also install Python. Macs come with Python installed (how cool is that?).

If you want to play with an online editor the one at Codecademy Labs is nice.