[Sugar-devel] [PATCH] Pippy example - multiplication table

Dinko Galetic dinko.galetic at gmail.com
Thu Jun 3 04:45:23 EDT 2010


This example neatly prints the multiplication table with dimensions the user
entered.


diff --git a/data/GSOC examples/multiplication table b/data/GSOC
examples/multip
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 i
+# 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"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.sugarlabs.org/archive/sugar-devel/attachments/20100603/9aefdaaf/attachment-0001.htm 


More information about the Sugar-devel mailing list