0

Can someone help me fix my prime generator?

It just sends a variety of errors that I don't undertand why. I just did a little bit of QBasic years ago so don't think I'm using much more than logic. requestedprimes = int(input('Requested number of primes: ')) listofprimes = [2] element = 3 listposition = 0 while requestedprimes < len(listofprimes): if element % listofprimes[listposition] == 0: element += 1 listposition = 0 else: if listposition < len(listofprimes) - 1: listposition += 1 else: element.append(listofprimes) listposition = 0 if requestedprimes == len(listofprimes) print(listofprimes) else: break # I need help with this

10th Oct 2016, 11:20 AM
Mario García
Mario García - avatar
2 odpowiedzi
+ 2
Python is very strict when it comes to whitespace! Everything from line 16 onwards (if requestedprimes == ...) should probably have four more spaces to the left of it, also on line 16 you are missing a ':' at the end. In case you did, also try not to mix spaces with tabs! After making these changes I got it to run, but it still doesn't do what it's supposed to I think, but that's for you to figure out :P
10th Oct 2016, 4:08 PM
Schindlabua
Schindlabua - avatar
+ 1
Woo I got it, in the first while it was >>> while len(listofprimes) < requested primes not the way I wrote it which skipped the loop from the beginning. And the last lines have to have the same intendents as the while loop because they are not in it, if they were they would have no sense. Thank you for spotting the ':' for me anyway :D Ps: and I prefer tabs for spaces, they're more clear in my opinion :p Ps2: I also put print(requestedprimes) on line 2 so it's visible and maybe I could do something like print(\nlistofprimes), I don't know if it'd work like that but you get my point haha.
10th Oct 2016, 5:21 PM
Mario García
Mario García - avatar