Open Side Menu Go to the Top
Register
** Python Support Thread ** ** Python Support Thread **

07-19-2011 , 02:19 PM
maybe consider starting at 2 for the hand values instead of 1

just cause its tilting me
** Python Support Thread ** Quote
08-18-2011 , 07:15 PM
Hi,

I'm following the MIT course about Python and am trying to write a script that can find the 1000th prime number. I saw that daveT posted his code for it earlier but didn't really read as to end up copying it.

I was wondering if anyone can give me pointers, so far I got this:

primesFound = 0
number = 1

while primesFound < 1000:
int((number/2)*2) = x
if x == x:
primesFound + 1
number + 2

I don't know how to set what numbers I want it to test, 1 to (?). Also, don't know how to make it repeat the process for each number (or is that what the ''while primesFound < 1000 is for?).

Help is appreciated.
** Python Support Thread ** Quote
08-18-2011 , 07:40 PM
use the [code] and [/code] tags to make your code readable on 2+2.

Start with something a little simpler. I usually write one to five lines at a time and then test it. That way, my program usually only has one "simple" mistake in it at any given time, even if it is thousands of lines long. Your program has multiple syntax errors, so you aren't doing this.

Could you make a program that prints out the first 1000 odd numbers, using the while loop? Once you have that right, you will see how the while loop works. Then you can move on to the next step.
** Python Support Thread ** Quote
08-18-2011 , 07:55 PM
Quote:
Originally Posted by RoundTower
use the [code] and [/code] tags to make your code readable on 2+2.

Start with something a little simpler. I usually write one to five lines at a time and then test it. That way, my program usually only has one "simple" mistake in it at any given time, even if it is thousands of lines long. Your program has multiple syntax errors, so you aren't doing this.

Could you make a program that prints out the first 1000 odd numbers, using the while loop? Once you have that right, you will see how the while loop works. Then you can move on to the next step.
Well I started with this:

Code:
 oddnumbers = 0
num = 1

while oddnumbers < 1000:
    if (num/2)/2 != 0:
        print num
I don't know how to write out the num + 2 part that comes after to make it try the next number. And should it be printing out the numbers every time it finds one that is odd or just save them to some kind of pool and print it all at the end?
** Python Support Thread ** Quote
08-18-2011 , 09:51 PM
Quote:
Originally Posted by MXdotCH
Well I started with this:

Code:
 oddnumbers = 0
num = 1

while oddnumbers < 1000:
    if (num/2)/2 != 0:
        print num
I don't know how to write out the num + 2 part that comes after to make it try the next number. And should it be printing out the numbers every time it finds one that is odd or just save them to some kind of pool and print it all at the end?
What is the value of num? How did it get to have that value? You can use the same technique to set it equal to itself plus 2.

Do you know any way to save all the numbers in "some kind of pool"? If you do, try that. If you don't, just printing them all out as you get to them would be a very good start.
** Python Support Thread ** Quote
08-18-2011 , 10:27 PM
Quote:
Originally Posted by RoundTower
What is the value of num? How did it get to have that value? You can use the same technique to set it equal to itself plus 2.

Do you know any way to save all the numbers in "some kind of pool"? If you do, try that. If you don't, just printing them all out as you get to them would be a very good start.
Do I only have to do this:

Code:
oddnumbers = 0
num = 1

while oddnumbers < 1000:
    if (num/2)/2 != 0:
        print num
        num = num + 2
And no I don't know how to save them into a pool. And this is probably a stupid question but how do I get the script to even start? If I do ''run module'' nothing happens?
** Python Support Thread ** Quote
08-18-2011 , 11:13 PM
Quote:
Originally Posted by MXdotCH
Do I only have to do this:

Code:
oddnumbers = 0
num = 1

while oddnumbers < 1000:
    if (num/2)/2 != 0:
        print num
        num = num + 2
And no I don't know how to save them into a pool. And this is probably a stupid question but how do I get the script to even start? If I do ''run module'' nothing happens?
You're on the right track, there are still some problems. But you need to be able to run it to see what happens, then you figure out what went wrong, then you figure out how to fix it.

You definitely need to have some way to run the script! Are you editing it in IDLE? If so just "run module" should work fine (you probably have to save it first). Or you can save the script in a file called something like oddnumbers.py, then go to the command line and type "python oddnumbers.py" to run it.

Incidentally, if the program looks like it is going on for ever, you can usually hit Ctrl-C to interrupt it.
** Python Support Thread ** Quote
08-18-2011 , 11:28 PM
Quote:
Originally Posted by RoundTower
You're on the right track, there are still some problems. But you need to be able to run it to see what happens, then you figure out what went wrong, then you figure out how to fix it.

You definitely need to have some way to run the script! Are you editing it in IDLE? If so just "run module" should work fine (you probably have to save it first). Or you can save the script in a file called something like oddnumbers.py, then go to the command line and type "python oddnumbers.py" to run it.

Incidentally, if the program looks like it is going on for ever, you can usually hit Ctrl-C to interrupt it.
I opened the IDLE (Python GUI) program, then clicked on the file tab and then "new window". I am writing stuff in there, then clicking on the run tab and then on run module. Nothing happens when I run it. And yeah, I had two divisions by two in there, second one should be a multiplication.
** Python Support Thread ** Quote
08-18-2011 , 11:46 PM
So you have two IDLE windows open -- one called Python Shell and one called whatever you saved your program as? If so, that's correct, I don't know why it isn't working. If you type stuff straight into the Python Shell window does it work? Try some simple expressions like "3 + 4", "a = 3", "print a".

or try closing IDLE and restarting it, something is wrong for you and you won't get anywhere without a way to test your work.
** Python Support Thread ** Quote
08-18-2011 , 11:47 PM
Quote:
Originally Posted by RoundTower
So you have two IDLE windows open -- one called Python Shell and one called whatever you saved your program as? If so, that's correct, I don't know why it isn't working. If you type stuff straight into the Python Shell window does it work? Try some simple expressions like "3 + 4", "a = 3", "print a".
Yep, that works. It's probably something missing from my script no? Is there some kind of line I'm missing just to make it start?
** Python Support Thread ** Quote
08-19-2011 , 12:04 AM
no, it should run as is! (although it won't do quite what you expect). Try replacing the whole file with one line that says
Code:
print "Hello world!"
and see if you can run that.

Actually, is it running, but just not responding, perhaps? Can you edit the file in IDLE after you hit "run module", or do you need to kill the process somehow?
** Python Support Thread ** Quote
08-19-2011 , 12:10 AM
Quote:
Originally Posted by RoundTower
no, it should run as is! (although it won't do quite what you expect). Try replacing the whole file with one line that says
Code:
print "Hello world!"
and see if you can run that.

Actually, is it running, but just not responding, perhaps? Can you edit the file in IDLE after you hit "run module", or do you need to kill the process somehow?
That print "Hello world!" works. Yea I can edit it after doing "run module". When I do it, Idle pops up, and the cursor focus goes down to the line between the >>> without me doing anything.

I don't know if it is running but just not responding. If it was, what should I do? By not responding do you mean that Idle just freezes and (not responding) appears by the window title? If so, no that's not appearing. This is weird.
** Python Support Thread ** Quote
08-19-2011 , 12:20 AM
OK. Your code will run forever without printing anything. For me, if that happens, IDLE won't respond (I can't edit anything in the editing window, and have to hit Ctrl-C or kill the process manually.)

I wasn't going to tell you any of the answers straight up, but to get over this bit, here's why your code runs forever:
Code:
while oddnumbers < 1000:
this says to do the bit inside the loop over and over again, so long as the value of oddnumbers is less than 1000. You set oddnumbers to 0, and you never change it, so it will always be 0, and the loop will go on for ever. Add a line somewhere inside the loop to say
Code:
oddnumbers = oddnumbers  + 1
And perhaps add a line at the end of the program (on the same level of indentation as the while) to say
Code:
print "finished!"
so you will know if it completes.

There are still errors in your program, but making sure it is running is the first step! I am going to go to bed, so you won't hear any more from me tonight -- just confirm if it now works for you.
** Python Support Thread ** Quote
08-19-2011 , 12:32 AM
Quote:
Originally Posted by RoundTower
There are still errors in your program, but making sure it is running is the first step! I am going to go to bed, so you won't hear any more from me tonight -- just confirm if it now works for you.
Yeah that's a dumb mistake. Here's what I changed:

Code:
oddnumbers = 0
num = 1
while oddnumbers < 1000:
    if (num/2)*2 != 0:
        oddnumbers = oddnumbers + 1
        print num
        num = num + 2

print "Finished!"
But even now, it does the same thing. I'll think about it, thanks for helping.

Edit: maybe the fourth line should be equal to num and not to 0...
Edit*: well now it worked...!
** Python Support Thread ** Quote
08-19-2011 , 03:56 PM
Quote:
Originally Posted by MXdotCH
I opened the IDLE (Python GUI) program, then clicked on the file tab and then "new window". I am writing stuff in there, then clicking on the run tab and then on run module. Nothing happens when I run it. And yeah, I had two divisions by two in there, second one should be a multiplication.
Open up only the IDLE shell. Go to open new Window. Now save that file (ctrl-S) and...

Now you have to save the file extension by hand, by actually typing

myFile.py

I know that the suffix is added on when you save this window, but that doesn't work for some stupid reason (using python 2.7, I don't know how 3 works).

Then go to 'file', 'open' and 'myFile.py' should be wherever you saved it. I just save mine on the desktop so it's easy to find. Next time you need it, just go to 'open recent' and it will be on the list.

**** You could also create the initial code in notepad and manually save it with a .py extension.

You can either run the code by going to file>>> run module or pressing F5.

To stop the Shell, use F6. Later on, when the problem sets get larger, you will find that the shell constantly crashes, but I think you'll figure that out in due time.
** Python Support Thread ** Quote
08-19-2011 , 04:02 PM
Good god, this problem set was such a nightmare. Once you get what they are trying to show you, the next few sets are easy sailing. It's just the painful lesson you must learn now.
** Python Support Thread ** Quote
08-19-2011 , 04:35 PM
Just to make finding help easier, here is the problem he is working on copy/pasted:

Problem 1.
Write a program that computes and prints the 1000
th
prime number.
Hints:
To help you get started, here is a rough outline of the stages you should probably follow in
writing your code:
1. Initialize some state variables
2. Generate all (odd) integers > 1 as candidates to be prime
3. For each candidate integer, test whether it is prime

1. One easy way to do this is to test whether any other integer > 1 evenly
divides the candidate with 0 remainder. To do this, you can use modular
arithmetic, for example, the expression a%b returns the remainder after
dividing the integer a by the integer b.


2. You might think about which integers you need to check as divisors –
certainly you don’t need to go beyond the candidate you are checking, but how much sooner can you stop checking?

4. If the candidate is prime, print out some information so you know where you are
in the computation, and update the state variables

5. Stop when you reach some appropriate end condition. In formulating this
condition, don’t forget that your program did not generate the first prime (2).
Use these ideas to guide the creation of your code.
If you want to check that your code is correctly finding primes, you can find a list of
primes at http://primes.utm.edu/lists/small/1000.txt.
** Python Support Thread ** Quote
08-19-2011 , 05:31 PM
Quote:
Originally Posted by daveT
Good god, this problem set was such a nightmare. Once you get what they are trying to show you, the next few sets are easy sailing. It's just the painful lesson you must learn now.
Yeah, it can be a bit confusing for me.

Right now I need to find out how to make it test if a number can only be divided by one and by itself..
** Python Support Thread ** Quote
08-20-2011 , 01:32 PM
Quick tip:

If you go to the site, underneath the lecture, there is a tab called 'resources.' There you have the handouts they give in the class. It shows all the code they show on the overhead.

I know that the code for testing even or odd was presented in class.
** Python Support Thread ** Quote
08-20-2011 , 01:53 PM
Quote:
Originally Posted by daveT
Quick tip:

If you go to the site, underneath the lecture, there is a tab called 'resources.' There you have the handouts they give in the class. It shows all the code they show on the overhead.

I know that the code for testing even or odd was presented in class.
Oh wow, thanks.
** Python Support Thread ** Quote
08-20-2011 , 04:52 PM
My turn to ask for help!

I posted this question on stackoverflow. It's about PyUnit, the unittest module. I'd copy and paste it here, but you may as well see it with the proper markup and any comments people leave.
** Python Support Thread ** Quote
08-20-2011 , 05:47 PM
Not sure if this is helpful, but we used nosetests to run all of our tests. There's a member variable you can set on the classes that tells nosetest to ignore it or not.

Nose in general was really useful.
** Python Support Thread ** Quote
08-20-2011 , 08:08 PM
Quote:
Originally Posted by jjshabado
Not sure if this is helpful, but we used nosetests to run all of our tests. There's a member variable you can set on the classes that tells nosetest to ignore it or not.

Nose in general was really useful.
Yeah, I've heard good things about nose, and must learn it some time.

I haven't come up against the problem of needing to ignore certain tests for some subclasses, but I know I will be in a spot where it would be handy. PyUnit also has a way to ignore classes (skip(), skipIf() etc as decorators, so it can determine at runtime what ones to skip) but only for Python 2.7, and the code I am working on needs to work for 2.6!
** Python Support Thread ** Quote
08-20-2011 , 08:19 PM
maybe i don't understand your problem but if the inputs are what vary, then shouldn't your tests be around the inputs? i don't understand why you need to make all these subclasses.

Code:
class Test_Foo(unittest.TestCase):
    def test_01_preserves_with_simple_args(self):
        """Foo preserves bazness correctly"""
        self.input = ""
        self.params = {}
        self.foo = Foo(self.input, self.params)
        self.assertTrue(is_preserved(self.foo.baz))
        ....
    ...

    def test_02_preserves_with_complex_args(self):
        """Foo preserves bazness correctly"""
        self.input = "complicated stuff"
        self.params = {"bar" : 3}
        self.foo = Foo(self.input, self.params)
        self.assertTrue(is_preserved(self.foo.baz))
        ....
    ...
and then refactor any duplication you don't like into a helper method.
** Python Support Thread ** Quote
08-20-2011 , 09:31 PM
No, the inputs stay the same for a large number of test cases, but the test logic is different.

In the example I have, imagine I have instead of test_01_... I have test_01 through test_30, all with the same inputs but testing different functionality of the Foo class. Then I want to rerun test_01 through test_30 with a different set of inputs (or possibly to test a different class, especially if it's a subclass of Foo, but that's another can of worms again).
** Python Support Thread ** Quote

      
m