Integers – weeks 3 and 4

“The fool wonders, the wise man asks.”

— Benjamin Disraeli

Handout: 2_Intro_1_integers.pdf

Training mission

Click on the student template. The teacher will help us fill it out.

Advanced (show me):Strings and integer are very different types; we use strings to create text while we use integers to count and do math. It would not make sense to have something like:

result = "hello" + 5

Sometimes, though we want to turn a string that contains an integer into an integer, to do some math, or to turn a number into a string. We can turn one type into the other using the str() and int() functions, i.e., str() turns something into a string and int() turns something into an integer. Hence, these do not work:

result = 5 + "10"                  # adding integers and strings does not work 
msg = "I am " + 10 + " years old"  # concatenation only works with strings

but these do:

result = 5 + int("10")                  # int("10") is the integer 10
msg = "I am " + str(10) + " years old"  # str(10) is the string "10"

3rd grade – Arithmetic with Python

Work it out

numbers-738068_640Python is really good at math; for Python is no more difficult to add 10-digit numbers than to add 1-digit numbers; the same goes for subtraction and multiplication. Hence, we are going to write a program to have him check our homework.

Let’s put two numbers in two variables at the beginning of our program, for example, in number_1 and number_2:

number_1 = 5


number_2 = 3

Then, we want a program that prints the following:

5 + 3 = 8
5 – 3 = 2
3 – 5 = -2
5 * 3 = 15

Answer: Show it to me

4th grade – Tooth-fairy arithmetic

Work it out

The tooth fairy brings a present to children when they lose a tooth; this present is sometimes money that is left under the child’s pillow. Please write a program that let us know how much would a child collect from the tooth fairy if she brings, say, $2 per tooth, given that a child has 20 teeth.

We can put the amount of the present and the number of teeth in variables, at the beginning of our program, and then get the program to print the result:

teeth = 20
present = $2
total = $40

Now to the difficult part of the problem: certain sharks lose 50000 (fifty thousand) teeth over their lifetime. Please, find out how much money would the tooth fairy give us if we were to lose 50000 teeth and if she did not bring us $2 but $5.

Answer: Show it to me

5th grade – Dungeons and Dragons

Work it out

Dungeons and Dragons is a game in which we take the role of humans, elves or dwarves, and go on adventures on a game board. We can explore lost ruins full of vampires, or tunnels with treasures guarded by dragons.

Did the dragon see us first or did we spotted it on time? I don’t know.. if you are a thief and have excellent perception skills you would have spotted the dragon first if you rolled 8 or more on a 20-side die but, if you are a paladin, chances are that you would need to roll a 19 or higher instead.

A 20-side die? Yes.. Dungeons and Dragons uses dice of 4, 6, 8, 10, 12 and 20 sides; here we are going to write a D&D dice roller. We will need to use the function randint(a,b) that returns a number between a and b, e.g.,

#  load the random library to give us access to randint
from random import randint   

roll = randint(5,10)         # use randint

will assign either 5, 6, 7, 8, 9 or 10 to the variable roll.

Your program must roll each of the 6 dice and print the resulting rolls in the following format:

You rolled a 2 with the 4-side die
You rolled a 5 with the 6-side die
You rolled a 3 with the 8-side die
You rolled a 10 with the 12-side die
You rolled a 13 with the 20-side die

Answer: Show it to me

6th grade – the sum of all the numbers up to 1,000,000

Work it out

Are you crazy? A million is a very big number.. how am I supposed to add all the numbers up to one million? Well.. let’s start with something smaller. How about adding all the numbers up to 6? Let’s start with the brute force approach:

1 + 2 + 3 + 4 + 5 + 6 = 21


This works but is not a very promising way to add a million numbers. Let’s try stacking the numbers one over the other. We will use the variable x to denote the value of the sum of all these numbers; for now we will pretend that we do not know its value.

+ 6

Since adding the numbers from 1 to 6 adds up to x, then adding the numbers twice should add up to 2x; we are going to add them to our column of numbers upside down, though; this does not make any difference to the result:

+ 6+1

Notice that all the rows add up to the same value, i.e., 7. In essence we are adding up 6 times the number 7:

+ 7

This means that 6 times 7 is 2x:

7 + 7 + 7 + 7 + 7 + 7 = 2x
6 * 7 = 2x


x = (6 * 7) / 2 # look at this formula carefully. How does it depend on 6?
  = 42 / 2
  = 21

This looks promising. Go over the same process on a piece of paper, but this time find the sum of all the numbers from 1 to 7.

Use this technique to write a program that tells us what is the sum of all the numbers from 1 to 1,000,000. As a test, this program should tell us that the sum of all the numbers from 1 to 25,000 is 312,512,500

Answer: Show it to me