Problem with returning prime number in a simple calculation program

QHorizon at gmail.com QHorizon at gmail.com
Sat Mar 3 18:36:36 EST 2007


Hello, I'm new to Python (I've learned everything up to iterators so
far) and fairly new to Programming.  This would be my first real
program:

#Coordinate Geometry (The whole program is not shown)

import math
import sys

print "Welcome to the Coordinate Geometry Calculator!"
print "Type 'terms' for a list of commands"

def main():
	print
	command = raw_input("Command? ")
	if command == "terms":
		terms()
		main()
	elif command == "distance":
		distance()
		main()
	elif command == "slope":
		slope()
		main()
	elif command == "endpoint":
		endpoint()
		main()
	elif command == "midpoint":
		midpoint()
		main()
	elif command == "prime":
		prime()
		main()
	elif command == "quit":
		sys.exit
	else:
		print "Not a valid command"
		main()

#...Declaring functions here...

def prime():
	num = input("Number ")
	i = num - 1
	divcounter = 0
	while i > 1:
		if num % i != 0:
			divcounter += 1
			i -= 1
	if divcounter == num - 2:
		print num, "is a prime number"
	else:
		print num, "is not a prime number"

#Start the program
main()

As it says in the title, I'm having trouble with the prime number
function.  It will print the sentence if the number is prime, but it
if isn't, the program goes into a state in the terminal where the
program never ends and you can just keep on typing.  Maybe the else
statement is ineffective?  Any ideas on how to fix this?




More information about the Python-list mailing list