Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 15th, 2005, 6:00 PM   #1
acab
Newbie
 
Join Date: Apr 2005
Posts: 7
Rep Power: 0 acab is on a distinguished road
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
acab is offline   Reply With Quote
Old Apr 15th, 2005, 6:22 PM   #2
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
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.
bl00dninja is offline   Reply With Quote
Old Apr 15th, 2005, 8:16 PM   #3
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
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.
Sane is offline   Reply With Quote
Old Apr 16th, 2005, 4:25 AM   #4
acab
Newbie
 
Join Date: Apr 2005
Posts: 7
Rep Power: 0 acab is on a distinguished road
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.
acab is offline   Reply With Quote
Old Apr 16th, 2005, 5:41 AM   #5
acab
Newbie
 
Join Date: Apr 2005
Posts: 7
Rep Power: 0 acab is on a distinguished road
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
acab is offline   Reply With Quote
Old Apr 16th, 2005, 6:56 AM   #6
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
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.
Sane is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:27 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC