Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/data/GSOC examples/multiplication table
blob: fd3c9881781e1d10f3d1f6debcef5fb7a6d87902 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import time
import os
import sys

print "This will draw the multiplication table with dimensions of your choice."
print "Feel free to modify and drawing speed and other variables."

# how many numbers per second? Default is 5.
drawing_speed = 5

# raw_input() will get whatever text you enter with your keyboard.
# int() will give an error if it can't turn that text into a number.
try:
    print "How many numbers wide?"
    x = int(raw_input())

    print "How many numbers high? "
    y = int(raw_input())
# If there's an error in the "try:" block of code, the following will happen.
# The program will print an error message and quit.
except:
    print "That is not a valid number."
    sys.exit()

# In case everything went well, the program didn't quit and is now here.
# The following will create two lists of numbers, each starting with 1 and
# ending with whatever you entered as x ("row" list) and y ("column" list).
# Practice: By default, both lists start with 1. What if we changed that?
row = range(1, x+1)
column = range(1, y+1)

# From now on, everything we want to print on the screen we'll store 
# in the "output" variable. First we make it an empty string, and then add to it
# whatever we want to print.
output = ""
# the first printed line, for decoration:
# add the beginning of it
output += "|-------|--" 
# for each number in a row, add eight dashes and mark the end of line with \n
# \n is called a 'newline' and it makes your console start writing a new line.
output += len(row) * "--------" +"\n" 
# What the second line starts with. \t marks one tab.
output += "|\t|\t"

# Now, we would like to print the first row of numbers, which
# represent the factors we'll multiply. Add each number from "row"
# to the output string. str(number) turns a number to characters
# (like number 42 to characters '4' and '2')
for number in row:
    output +=  str(number) + "\t"

# add another decorative line
output += "\n" + "|-------|--" + len(row) * "--------"  + "\n"

# for each number in the first column, multiply it with each number in the 
# first row. One by one, add the results to "output" and print it.
for factor1 in column:
    output += "|   " + str(factor1) + "\t|\t"
    for factor2 in row:
        output +=  str(factor1*factor2) + "\t"
        # clear the screen from what was last printed (old output) so 
        # we can print the new output (with one result added)
        os.system('clear')
        print output
        # Pause the program. If "drawing_speed" is 5, it will pause for 
        # 1/5 seconds (0.2 seconds), which gives us five characters 
        # per seconds.
        time.sleep(1.0 / drawing_speed)
    # mark the end of the line
    output += "\n"