![]() |
|
Re: ** Python Support Thread **
maybe consider starting at 2 for the hand values instead of 1
just cause its tilting me :D |
Re: ** Python Support Thread **
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. |
Re: ** Python Support Thread **
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. |
Re: ** Python Support Thread **
Quote:
Code:
oddnumbers = 0 |
Re: ** Python Support Thread **
Quote:
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. |
Re: ** Python Support Thread **
Quote:
Code:
oddnumbers = 0 |
Re: ** Python Support Thread **
Quote:
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. |
Re: ** Python Support Thread **
Quote:
|
Re: ** Python Support Thread **
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. |
Re: ** Python Support Thread **
Quote:
|
Re: ** Python Support Thread **
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!"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? |
Re: ** Python Support Thread **
Quote:
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. |
Re: ** Python Support Thread **
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:Code:
oddnumbers = oddnumbers + 1Code:
print "finished!"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. |
Re: ** Python Support Thread **
Quote:
Code:
oddnumbers = 0Edit: maybe the fourth line should be equal to num and not to 0... Edit*: well now it worked...! |
Re: ** Python Support Thread **
Quote:
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. |
Re: ** Python Support Thread **
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.
|
Re: ** Python Support Thread **
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. |
Re: ** Python Support Thread **
Quote:
Right now I need to find out how to make it test if a number can only be divided by one and by itself.. |
Re: ** Python Support Thread **
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. |
Re: ** Python Support Thread **
Quote:
|
Re: ** Python Support Thread **
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. |
Re: ** Python Support Thread **
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. |
Re: ** Python Support Thread **
Quote:
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! |
Re: ** Python Support Thread **
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): |
Re: ** Python Support Thread **
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). |
| All times are GMT -4. The time now is 03:50 PM. |
|
Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Copyright © 2008-2020, Two Plus Two Interactive