I thought I understood how tuples work, but that is not the case.

lose = ['skldhf', 'laweoirht', 'skdljhflkjhs']
print lose

How would I get it to print of the three phrases randomly? No, those aren't what I will be actually using, I just haven't come up with what I want to use yet.

Thanks for the help.

Recommended Answers

All 6 Replies

First of all lose is not a tuple its a list a tuple is declared as follows

lose = ('fdasf','dfasf','asdf')

for pseudo random numbers you import the random module

i =  range(len(lose))
random.shuffle(i)

The values of i are now shuffled and can be used to access the tuple in "random" order

Thanks.

Is it possible to make a tuple out of functions instead of 'slkdhfk'?

Thanks.

What, you mean like this?

>>> def funA():
...     print 'A'
...     
>>> def funB():
...     print 'B'    
...     
>>> def funC():
...     print 'C'
...     
>>> my_tup = ( funA, funB, funC )
>>> for each_function in my_tup:
...     each_function()
...     
A
B
C
>>>

To explain the previous code a bit clearer

i = range(len(lose))

This returns a list to i so if lose is

lose = ('efesef','esfes','asefe')

then i is

i = [0,1,2]

the code random.shuffle(i) is just shuffling the number
so you can index different items in the list for example after
random.shuffle(i) is called i may look like this [2,0,1]

Tuple elements can be accessed by their index:

my_tup = ('fdasf','dfasf','asdf')
print my_tup[0]  # --> fdasf
print my_tup[2]  # --> asdf

Thanks for all the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.