Well, I've been picking at learning python, got tired of reading, and figured I'd try to replicate my prime number generator I wrote (with much TSDN forum help) in C++. I've hit a stumbling block... the program is supposed to print onscreen all the prime numbers between two numbers given to it, so if I put in 1 and 10, it should print out 1, 3, 5, 7 (I know, technically 1 isn't considered prime, and 2 should be on there, but otherwise...) Which it does, but then IDLE freezes and needs to be killed with system monitor (in ubuntu 6.06)... If it helps, I am using Python v. 2.4.3
And the code:
Thanks for all the help so far!
And the code:
Code:
print print print "This program identifies all the prime numbers" print "between two given numbers" print # I just wanted the start function to take two integers # and pass them to prime_test, with num1 as testnum and # num2 as maxnum. def start(): num1 = input("Please enter the lower number: ") num2 = input("Please enter the higher number: ") if num1 > num2: print "Please input the lower number first." start() elif num1 == num2: print "Please use 2 different numbers." start() elif (num1 < 0) or (num2 < 0): print "Please use positive integers only please." start() prime_test(num1, num2) # I'm not sure where the problem is, but I think it # is in this function. testnum starts off as the lowest # number to test, passed from num1 in the start function # and increases until it reaches the maximum number of # maxnum, passed from num2 in the start function. def prime_test(testnum, maxnum): import math count = 3 prime = True while testnum <= maxnum: if testnum % 2 == 0: testnum = testnum + 1 elif testnum % 2 != 0: while count <= math.sqrt(testnum): if testnum % count == 0: prime = False else: count = count + 2 if prime == True: print testnum testnum = testnum +1 start() print "Do you want to start again?" print "Type X and hit enter to exit," endprompt = raw_input("type anything else and press enter to start again: ") if endprompt == 'x' or 'X': print print print "Goodbye!" else: start()
Comment