Can't Randomize List With Classes Inside Of It Python 2.7.4
Solution 1:
You need to call the method on the class returned by random.choice(the_shuffler)
.
It would help if each class had the description printing method named the same.
Solution 2:
Somehow you have the syntax the wrong way round. You create a dictionary with class
-instances as keys and strings
as values.
If you want to call functions/classes randomized you have to assign a variablename to each function, randomize the order of these names and call the returned function by applying () to it.
If you want to use classes or functions as values of your dict, does not really matter - for classes you would also somehow have to store which method to call (or simply print the class and code a working str() for it).
My example uses functions directly:
defa():
print"a"defb():
print"b"defc():
print"c"defd():
print"d"defe():
print"e"import random
# you would have to incoporate this somewhere into your program logic.# probably calling it from Rules() - somehow. defmenu():
# map functions to keys# "a" is a string, the name for a function# a is the function# a() calls the function, as does options["a"]()
options = { "a" : a, "b" : b, "c" : c, "d" : d, "e" : e}
# get all possible names
listOfKeys = list(options.keys())
# shuffle in place into random order
random.shuffle(listOfKeys)
# visit them in this orderfor r in listOfKeys:
options[r]() # get function-value from the dict and () it print"First try:"
menu()
print"\n\nSecond try:"
menu()
Output:
Firsttry:
ecdabSecondtry:
bcaed
Link to docu for random.shuffle()
Why using classes here would benefit your code is not clear to me...
Post a Comment for "Can't Randomize List With Classes Inside Of It Python 2.7.4"