How would I make a random hexdigit code generator using .join and for loops?

2024/10/1 3:23:18

I am new to programming and one assignment I have to do is create a random hexdigit colour code generator using for loops and .join. Is my program below even close to how you do it, or is it completely off? And, is there a way to make a random amount of numbers and letters appear within 6?

import random
str = ("A","B","C","D","E","F","G","H")seq = ("1","2","3","4","5","6", "7","8","9")print '#',
for i in range(0,3):letter = random.choice(str)num = random.choice(seq)print num.join(letter),print letter.join(num)
Answer

Strings can be iterated over, so my code would look like this.

import randomdef gen_hex_colour_code():return ''.join([random.choice('0123456789ABCDEF') for x in range(6)])if __name__ == '__main__':print gen_hex_colour_code()

results in

In [8]: 9F04A4In [9]: C9B520In [10]: DAF3E3In [11]: 00A9C5 

You could then put this in a separate file called for example, myutilities.py

Then in your main python file, you would use it like this:

import myutilitiesprint myutilities.gen_hex_colour_code()

The if __name__ == '__main__': part will only get executed if you run the myutilities.py file directly. It will not execute when you import it from another file. This is generally where testing functions go.

Also, note that this is using the syntax for Python 2.7. In Python 3.0, one major difference is that print is a function and you would have to use print(gen_hex_colour_code()) instead. See http://docs.python.org/3.0/whatsnew/3.0.html for more info on how things are different if you are confused.

Why would I still be using Python 2.7? Many scientific python modules are still using the 2.7 variant, but for a newbie to Python, I would suggest you stick with 3.0

https://en.xdnf.cn/q/71007.html

Related Q&A

Multiline python regex

I have a file structured like this : A: some text B: more text even more text on several lines A: and we start again B: more text more multiline textIm trying to find the regex that will split my file …

How can I process xml asynchronously in python?

I have a large XML data file (>160M) to process, and it seems like SAX/expat/pulldom parsing is the way to go. Id like to have a thread that sifts through the nodes and pushes nodes to be processed …

python postgresql: reliably check for updates in a specific table

Situation: I have a live trading script which computes all sorts of stuff every x minutes in my main thread (Python). the order sending is performed through such thread. the reception and execution of …

How to push to remote repo with GitPython

I have to clone a set of projects from one repository and push it then to a remote repository automatically. Therefore im using python and the specific module GitPython. Until now i can clone the proje…

How do I do use non-integer string labels with SVM from scikit-learn? Python

Scikit-learn has fairly user-friendly python modules for machine learning.I am trying to train an SVM tagger for Natural Language Processing (NLP) where my labels and input data are words and annotatio…

Python - walk through a huge set of files but in a more efficient manner

I have huge set of files that I want to traverse through using python. I am using os.walk(source) for the same and is working but since I have a huge set of files it is taking too much and memory resou…

Python: handling a large set of data. Scipy or Rpy? And how?

In my python environment, the Rpy and Scipy packages are already installed. The problem I want to tackle is such:1) A huge set of financial data are stored in a text file. Loading into Excel is not pos…

Jupyter notebook - cant import python functions from other folders

I have a Jupyter notebook, I want to use local python functions from other folders in my computer. When I do import to these functions I get this error: "ModuleNotFoundError: No module named xxxxx…

Can pandas plot a time-series without trying to convert the index to Periods?

When plotting a time-series, I observe an unusual behavior, which eventually results in not being able to format the xticks of the plot. It seems that pandas internally tries to convert the index into …

pip install syntax for allowing insecure

I tried to run$pip install --upgrade --allow-insecure setuptoolsbut it doesnt seem to work? is my syntax wrong?this is on ubuntu 13.10 I need --allow-insecure as I havent been able to the get the co…