2 Replies - 1194 Views - Last Post: 31 January 2010 - 10:29 PM Rate Topic: -----

#1 TheStuff   User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 16-April 09

Primality Tester

Post icon  Posted 26 January 2010 - 05:42 AM

Hey there,

I'm learning Python for 52 weeks of code, and this is going to be my submission. Sadly, it's not working. It's reading the input() as a string, which is STRANGE because I'm entering numbers. I'm using Python 3.0.1 so for some reason raw_input() doesn't work. Is there a way to force it to read input() as an int?

Code:
number = input('Enter a number, and I will print all prime numbers below it: ')
up = 5
while up <= number:
	  whileup = 2
	  isPrime = False
	  while whileup < number:
			if number%whileup!=0:
				  whileup = whileup + 1
			elif number%whileup==0:
				  break
			elif whileup > (number/2):
				  isPrime = True
				  break
	  if isPrime == True:
			print ("Prime Number: ", number)
			isPrime = False
	  number = number + 1



Please excuse any obvious efficiency problems. This is my first Python code ever.

Is This A Good Question/Topic? 0
  • +

Replies To: Primality Tester

#2 girasquid   User is offline

  • Barbarbar
  • member icon

Reputation: 109
  • View blog
  • Posts: 1,825
  • Joined: 03-October 06

Re: Primality Tester

Posted 26 January 2010 - 11:02 AM

You could try wrapping your input() call in a call to int(), to cast it as an integer - don't forget to catch the exception if it fails.
Was This Post Helpful? 0
  • +
  • -

#3 atraub   User is offline

  • Pythoneer
  • member icon

Reputation: 837
  • View blog
  • Posts: 2,271
  • Joined: 23-December 08

Re: Primality Tester

Posted 31 January 2010 - 10:29 PM

Not a big problem. You should know that in python 3k, raw_input and input have been replaced by input. input by default will always yield a string. As girasquid said, you can cast it to an integer. the line written in that format will look like this:
number = int(input('Enter a number, and I will print all prime numbers below it: '))



-Adam
http://www.learningp...rogramming.com/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1