Specter0420
  • Specter0420
PS1 Help. I am breaking the problem down into smaller steps. I wrote this code http://codepad.org/9moWoTPF to determine is a number is prime or not. It works but when the answer is not prime it continuously prints not prime.
MIT 6.00 Intro Computer Science (OCW)
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
jamiebookeater
  • jamiebookeater
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
Specter0420
  • Specter0420
PS. Is there a better, cleaner looking way to code this? Without putting int( ) so often?
maitre_kaio
  • maitre_kaio
What happens if, in the while loop, you see that is not a prime ? You print 'Not Prime', and continue to loop. So it never stops... You need to break the loop after the print statement. Also, you don't have to always cast to int like you are doing ! You know div is an integer since you are initialzing it to 2 and incrementing by 1 after that. You need to check t4p, but just once, before entering the while loop.
Specter0420
  • Specter0420
I am not sure what you mean by "You need to check t4p, but just once, before entering the while loop" check it for what? My new code is still having problems, http://codepad.org/G2TnigvI

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

maitre_kaio
  • maitre_kaio
Please explain what the problem is.
Specter0420
  • Specter0420
If not prime it prints not prime then prime. Does while work with else like if and else?
Specter0420
  • Specter0420
Is there a website that can run/test python code. I am on a WP7 smartphone for the next couple hours? This site works like garbage on it too btw. The only website I have had trouble with in 1.5 years.
anonymous
  • anonymous
Consider using a flag which is just a variable you can use to set as true or false and then use later in the program to make a comparison to. In your example the while loop is figures out if a number is not prime and then prints out "not prime". After it exits the while loop it then proceeds to the next statement which is your print statement: print("Prime") the above statement will be executed every time no matter what, so how do you avoid this? at the start you have a flag: isPrime = True in your while loop you can set your flag instead of making a print statement isPrime = False and at the end do a comparison if isPrime == True: print("The number is prime") else: print("The number is not prime")
maitre_kaio
  • maitre_kaio
kl365 is right. But for your information, there is also in Python the while...else construct: http://docs.python.org/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops So you could use it. I don't recommend it because it doesn't exist in any other language I know.
Specter0420
  • Specter0420
Thanks guys, I couldn't read the last two posts on my phone (they were covered up by a "start a new group" box that i couldn't close.) Also when i typed on my phone the view would move from the text field to a spot at the bottom and back to the text field with every character, and the delay from when I pressed a button until the text showed up was 5-10 seconds. I have never seen this on any other website. Time to modernize openstudy! Hopefully you will know where to pass this along to.
Specter0420
  • Specter0420
I used the while/if method and it works great, only had to add one line consisting of one word. I now see you don't recommend it so i am going to try the flag, even though I don't think I am supposed to know about it yet. PS. codepad is down on my two computers AND phone so I am pretty sure it is on their end so I am attaching my python file. Could you tell me if i am still casting int ( )too often or anything else you notice in any of my other programs.
1 Attachment
anonymous
  • anonymous
This is where someone more familiar with python itself might be able to provide more insight. One thing to note, there is no need to uses parentheses each time you use a variable only if you are explicitly casting it to another type. ex1: (div) = (div) + 1 can be written as div = div + 1 example with casting: t4p = raw_input(...) will assign t4p the string value of whatever the user enters. In order for us to do mathematical calculations we need t4p to be an integer so we do the following: while div <= int(t4p) / 2 This uses the int() function to take whatever is supplied (in this case t4p), converts it to an integer and returns that value. to avoid having to cast t4p to integer every time you need to use it, common practice is to cast in the initial assignment as follows: t4p = int(raw_input(...)) Of course this is not secure in any way and doesn't validate the input which is something that will be covered later I'm sure.
1 Attachment
Specter0420
  • Specter0420
Thank you very much! You both cleared up a lot for me. I knew about not needing the ( )'s I just forgot to deleted them after deleting all my unnecessary int's (i am doing this at work in my spare time, on and off a lot!). I am going to rewrite this program using t4p = int(raw_input(...)) (and throw in some defensive programming) and try using flags because I only plan to LEARN with python.

Looking for something else?

Not the answer you are looking for? Search for more explanations.