![]() Then, how to prompt the user for the number of words to generate at random.įrom there, you can make it as simple or complex as you like, maybe make the input and output cleaner and/or add some color, or find a better randomization algorithm, faster/better/more elegant ways of accessing the list, rules about the words that are returned, etc.Ĭlone this project’s repo and play with it. Now, figure out how to take some number of words and get that many words at random. Usually from the foundational pieces to the top.įor this, you might work through it like: figure out how to get a random value, then how to get a word instead of that value (like taking a word from a list if given a random integer, for instance). However, I've looked at code I wrote just a year ago and had to wonder why I wrote it that way.Įat the elephant a bite at a time break the problem down into small pieces starting where you feel is best and iterate from there. If it's a one-off script just for you, readability is less of a concern. What if I put in a negative number? 0? or letters? What if the URL is unreachable?īTW, this is coming from my experience working in a corporate environment, but I think this should be the mindset of any code you want others to collaborate on. I know this is beginner code and maybe you haven't learned it yet, but whenever you are handling user input or any external data, you should sanitize and/or validate it. However, if you still want to use regex, look into regex capture groups because you can use a single regex operation instead of the two you are using now. I won't give the code for this, but take a look at splitlines() and dict() and split(). Then you can do a simple key lookup from the dice roll. IMO, the regex is overkill and you can just create a dictionary from the content. Here's an example using the secrets modules from my earlier point and code that combines a for loop and generator expression that creates your wordlist (I named mine dice_rolls: DICE_COUNT = 5ĭice = ''.join(str(secrets.randbelow(6) + 1) for _ in range(DICE_COUNT)) Sometimes a for loop better describes your code. List comprehensions are amazing and I went crazy with them when I first started learning python, but just because you can, doesn't mean you have to. The variable names are not really describing what they are used for and the list comprehension is confusing. It took me too long to understand what you were doing with final_list and wordlist. If you're using 3.6+, they have a built in module now: secrets Introduction to Programming with Python (from Microsoft Virtual Academy)Īs someone pointed out, the random module is not suitable for cryptographic purposes./r/git and /r/mercurial - don't forget to put your code in a repo!./r/pyladies (women developers who love python)./r/coolgithubprojects (filtered on Python projects). ![]() /r/pystats (python in statistical analysis and machine learning)./r/inventwithpython (for the books written by /u/AlSweigart)./r/pygame (a set of modules designed for writing games)./r/django (web framework for perfectionists with deadlines). ![]() /r/pythoncoding (strict moderation policy for 'programming only' articles).NumPy & SciPy (Scientific computing) & Pandas.Transcrypt (Hi res SVG using Python 3.6 and turtle module).Brython (Python 3 implementation for client-side web programming). ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |