![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Apr 2005
Posts: 7
Rep Power: 0
![]() |
Newbie quesiton: variable assigment
In the Python Shell:
newnum = num % 2 works fine, but when used in a super simple script it get an error: newnumb = num % 2 TypeError: not all arguments converted during string formatting print "This program checks to see if a number is prime, "
num = raw_input("Choose a number: ")
newnumb = num % 2
if newnumb != 0:
print "The number ", num, " is not a prime. "
else:
print "Congrats! The number ", num, " is a prime. "Just learnign Python, thanks in advance acab |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
you might want to examine your algorithm...it looks like it screens for even numbers, not prime numbers. i don't really know anything about python, but the error sounds like it's accepting num as a string value rather than an integer value. i don't know if you need to use another function, or explicitly convert the data type, or if i'm just plain wrong. but no, anything % 2 that equals 0 is even, not prime.
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. Last edited by bl00dninja; Apr 15th, 2005 at 6:25 PM. |
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,798
Rep Power: 5
![]() |
Well assigning a variable to num % 2 is kind of pointless, you should just put that straight into the if statement
"if num%2 != 0:" The reason it errors is because raw_input assigns the input to a string. And you tried to handle it as an integer. You can either put int( variable letter) wherever you handled it, or change raw_input to just input to make it accepted as an integer from the beginning. Secondly, Primes aren't just decided if it isn't equally divisible by 2. You have to loop it through all the possibilities from 2 to x, where the loop value is checked for a remainder. Hope that helped. |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Apr 2005
Posts: 7
Rep Power: 0
![]() |
Thank you bloodninja and Sane, you know I'm so new to programing that I hadn't even reached the fundamental point of "Is this even doing what I'm wanting it to do, that being checking for primes", I was stuck on "sure seems like this should work, scratch scratch". I actually did do it all in the 'if' statement but that errored as well so I changed it attempting to troubleshoot it. You were right on Sane, once I removed the 'raw_' it worked fine.
(is raw_input == string, and input == ints, floats...?) Thanks fot the help yal. acab PS Also thanks for the tip on checking for primes, now that I can focus on that problem. |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Apr 2005
Posts: 7
Rep Power: 0
![]() |
This seems to do the trick...
print "This program checks to see if a number is prime, "
num = input("Choose a number: ")
count = 2
while count <= num:
if num % count == 0:
print num, " is not prime."
break
elif num % count != 0:
count = count + 1
if count == num:
print num, " is prime."
break |
|
|
|
|
|
#6 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,798
Rep Power: 5
![]() |
Very good, that's not even the way I was thinking, but it works very nicely. Good job problem solving.
The final thing is to make the program more user-friendly. If it's not prime, the user might want to know why. Print which number caused the loop to break, to show which number it is equally divisible by. P.S. Good catch on starting count at 2, because every number is divisible by 1. It could have taken a while to realise that error while debugging the problem. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|