Basic Multiplication in Python

import sys

def main(argv):

	if len(argv) != 2:
		sys.exit('Usage: <a> <b>')
	a = int(sys.argv[1])
	b = int(sys.argv[2])
	print "The result of " + str(a) + " times " + str(b) + " is: " + str(a*b)

if __name__ == "__main__":

  • Jonathan

    How would you rewrite this without (a*b) in line 11? ie what if your computer did not know its 'times tables'? Can you code multiplication via another way and explain it line by line as if talking to a 10 year old? Now THAT is a challenge!

    • CptDeadBones

      Thanks for the reply.

      Instead of (a*b) you can explain multiplication as repetitive addtion. That is if a=4 and b=3, you add 4, 3 times. 4+4+4. In Python you can do this with a for loop.

      a = 4
      b = 3
      c = 0
      for i in range(b):
      c = c + a
      print c

      That should get it done. Good Luck!