Description: update python scripts for python3 Author: tony mancill --- a/primes/matho-sum +++ b/primes/matho-sum @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 # Python program to sum many large integers separated by spaces or newlines. # The integers to sum may be entered on the command-line or into standard input. @@ -12,13 +12,13 @@ # read stdin if no command line args while True: try: - input_line = raw_input() + input_line = input() except: break; - for s in string.split(input_line): + for s in input_line.split(): sum += int(s) else: # sum together the command-line args for arg in args: sum += int(arg) -print sum +print(sum) --- a/primes/matho-mult +++ b/primes/matho-mult @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 # Python program to multiply many large integers separated by spaces or newlines. # The integers to multiply may be entered on the command-line or into standard input. @@ -15,13 +15,13 @@ # read stdin if no command line args while True: try: - input_line = raw_input() + input_line = input() except: break; - for s in string.split(input_line): + for s in input_line.split(): prod *= int(s) else: # multiply together the command-line args for arg in args: prod *= int(arg) -print prod +print(prod) --- a/primes/primorial +++ b/primes/primorial @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 # This is a Python program to display large primorials. # A primorial is the product of all primes up to the given number. @@ -16,11 +16,11 @@ import sys def usage(ev): - print "This program calculates large primorials." - print - print "Usage: %s integers" % os.path.basename(sys.argv[0]) - print - print "A primorial is the product of all primes up to the given number." + print("This program calculates large primorials.") + print() + print("Usage: %s integers" % os.path.basename(sys.argv[0])) + print() + print("A primorial is the product of all primes up to the given number.") sys.exit(ev) def output_primorial(arg): @@ -36,9 +36,9 @@ for arg in args: try: if (int(arg) < 1): - print >>sys.stderr, "Number too small." + print("Number too small.", file=sys.stderr) sys.exit(1) except: - print >>sys.stderr, "Positive integer required." + print("Positive integer required.", file=sys.stderr) usage(1) output_primorial(arg) --- a/examples/factorial +++ b/examples/factorial @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 # This is a Python program to display large factorials and test "fact.py". @@ -8,13 +8,13 @@ import string def usage(): - print "This program calculates large factorials." - print "Requires and tests \"fact.py\"." - print - print "Usage: %s integer_expressions" % os.path.basename(sys.argv[0]) - print - print "The integer expressions should be separated by spaces." - print "A factorial is the product of all positive integers <= a given integer." + print("This program calculates large factorials.") + print("Requires and tests \"fact.py\".") + print() + print("Usage: %s integer_expressions" % os.path.basename(sys.argv[0])) + print() + print("The integer expressions should be separated by spaces.") + print("A factorial is the product of all positive integers <= a given integer.") sys.exit(2) args = sys.argv[1:] @@ -23,8 +23,8 @@ else: try: num = eval(string.join(args)) - print "factorial(", num, ") =", factorial(num) + print("factorial(", num, ") =", factorial(num)) except: for arg in args: num = eval(arg) - print "factorial(", num, ") =", factorial(num) + print("factorial(", num, ") =", factorial(num)) --- a/examples/fact.py +++ b/examples/fact.py @@ -7,7 +7,7 @@ def factorial(x): "Return x! (x factorial)." if (x < 0 or (x % 1.0) != 0.0): - raise ValueError, "Factorial argument must be a positive integer." + raise ValueError("Factorial argument must be a positive integer.") if (x == 0): return x + 1 d = x @@ -15,6 +15,6 @@ x -= 1 temp = d * x if (temp <= d): - raise ValueError, "Factorial result too large." + raise ValueError("Factorial result too large.") d = temp return d