Simple prime number generator in Python

There are some problems: Why do you print out count when it didn’t divide by x? It doesn’t mean it’s prime, it means only that this particular x doesn’t divide it continue moves to the next loop iteration – but you really want to stop it using break Here’s your code with a few fixes, it prints … Read more

Playing mp3 song on python

Try this. It’s simplistic, but probably not the best method. Please note that pygame’s support for MP3 is limited. Also, as pointed out by Samy Bencherif, there won’t be any silly pygame window popup when you run the above code. Installation is simple – Update: Above code will only play the music if ran interactively, since the play() call will execute instantaneously … Read more

Generate random colors (RGB)

Here: The result is either red, green or blue. The method is not applicable to other sets of colors though, where you’d have to build a list of all the colors you want to choose from and then use random.choice to pick one at random.

Django – no such table exception

I solved the same problem with these steps : Delete your database (db.sqlite3 in my case) in your project directory Remove everything from __pycache__ folder under your project subdirectory For the application you are trying to fix, go to the folder and clear migrations and __pycache__ directories When you are sure you have cleared all the above files, run: I hope this … Read more

Converting XML to JSON using Python?

There is no “one-to-one” mapping between XML and JSON, so converting one to the other necessarily requires some understanding of what you want to do with the results. That being said, Python’s standard library has several modules for parsing XML (including DOM, SAX, and ElementTree). As of Python 2.6, support for converting Python data structures to and from JSON … Read more