Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDinko Galetic <dgaletic@everflame.(none)>2010-06-03 07:38:36 (GMT)
committer Dinko Galetic <dgaletic@everflame.(none)>2010-06-03 07:38:36 (GMT)
commitb42aaca7f79dcd31b349791e1a13183758bf59a9 (patch)
treeaf821dd3a85a29a106049a19ce37deb045099615
parent14e9dd363e755a00b5fc7f611b7cd2698bb5fb4f (diff)
Added a Python script which prints a multiplication table in a neat way.
It uses time.sleep() to pause between prints so the user gets a "flowing" output.
-rw-r--r--data/GSOC examples/multiplication table69
1 files changed, 69 insertions, 0 deletions
diff --git a/data/GSOC examples/multiplication table b/data/GSOC examples/multiplication table
new file mode 100644
index 0000000..4601f01
--- /dev/null
+++ b/data/GSOC examples/multiplication table
@@ -0,0 +1,69 @@
+import time
+import os
+
+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"