random from an array

C

Calvin79

Hi All,

I've just started to try python so forgive the ignorance!

I've been using this, or something like it;

First = random.choice ('abc')
answer = first
print answer - no problems there.

What I want to do, however, is take a random pick from something like the
following;

First = random.??? ("rna", "rnb", "rnc") - this of course "random.choice"
doesn't work.

Could some kind person help me out with the code? I know I'm trying to run
before I can walk, but there you go...

Thanks,

Calvin
 
P

Peter Hansen

Calvin79 said:
I've just started to try python so forgive the ignorance!

I've been using this, or something like it;

First = random.choice ('abc')

This works not because choice() takes a string, but because
even though it takes a sequence, a string *is* a sequence
(of bytes).
What I want to do, however, is take a random pick from something like the
following;

First = random.??? ("rna", "rnb", "rnc") - this of course "random.choice"
doesn't work.

Just use another sequence, either a tuple or a list:

random.choice(("rna", "rnb", "rnc"))
or
random.choice(["rna", "rnb", "rnc"])

The latter is preferred for various mostly stylistic reasons...

-Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top