Can';t使用Python 2.7.4中的类将列表随机化

Can';t使用Python 2.7.4中的类将列表随机化,python,python-2.7,list,Python,Python 2.7,List,我是新的编码,我需要一些帮助。我正试图在文本冒险中随机化这些房间或“场景”,但每当我尝试随机化时,它们甚至在我运行时都不会出现!以下是脚本: from sys import exit import time import random #import another file here to use in this class Scene(object): def enter(self): print "Not yet configured." class Sta

我是新的编码,我需要一些帮助。我正试图在文本冒险中随机化这些房间或“场景”,但每当我尝试随机化时,它们甚至在我运行时都不会出现!以下是脚本:

from sys import exit
import time
import random
#import another file here to use in this

class Scene(object):
    def enter(self):
        print "Not yet configured."



class Start():

    def start(self):
        print "Hello. How are you? You are about to play a game that is set in a crazy world."
        print "We are creating your profile right now."
        epic = raw_input("Enter your name here: ")
        print "Hello, %s." % epic
        print "You are being transported to a randomly generated world. Try to survive as long as possible."
        print "Here's some advice, %s: Don't die. Make the right choice. Be careful." % epic
        print "The rules will be shown to you soon."
        print "LOADING..."
        time.sleep(1)
        return Rules().rules()

class Rules(Scene):

    def rules(self):
        print ""
        print "-------------"
        print ""
        print "These are the rules:"
        print "1: Be a good sport. This game takes skill and memory to be able to win, so try your best to succeed."
        print "2: Every time you die, you do not get to respawn, so you will be prompted to either just not play anymore"
        print "or play again. If you decide to play again, you will most likely be on a new world with a new puzzles."
        print "3: Finally, have fun. Hopefully this game brings you joy, so have a great time playing it."
        return random.choice(the_shuffler)

class BoilerRoom(Scene):

    def boiler_room(self):
        print "You are in the boiler room."

class Kitchen(Scene):

    def kitchen(self):
        print "You are in the kitchen."

class Pool(Scene):

    def pool(self):
        print "You are in the pool."

class TennisCourts():

    def tennis_courts(self):
        print "You are in the tennis courts."

class SoccerField():

    def soccer_field(self):
        print "You are on the soccer field."

class Map(object):

    scenes = {
        Rules(): 'rules',
        BoilerRoom(): 'boiler_room',
        Kitchen(): 'kitchen',
        Pool(): 'pool',
        TennisCourts(): 'tennis_courts',
        SoccerField(): 'soccer_field'
    }

the_shuffler = (BoilerRoom, Kitchen, Pool, TennisCourts, SoccerField)

Start().start()

您需要对
random.choice(洗牌器)
返回的类调用该方法


如果每个类都有名称相同的描述打印方法,这会有所帮助。

不知何故,语法错误。创建字典时,将
-实例作为键,将
字符串
作为值

如果要随机调用函数/类,则必须为每个函数分配一个variablename,将这些名称的顺序随机化,并通过对返回的函数应用()来调用该函数

如果您想使用类或函数作为dict的值,这并不重要——对于类,您还必须以某种方式存储要调用的方法(或者只需打印类并为其编写一个有效的str

我的示例直接使用函数:

def a():
    print "a"

def b():
    print "b" 

def c():
    print "c"

def d():
    print "d"

def e():
    print "e"



import random


# you would have to incoporate this somewhere into your program logic.
# probably calling it from Rules() - somehow. 

def menu():
    # 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 order
    for r in listOfKeys:  
        options[r]() # get function-value from the dict and () it 


print "First try:" 
menu()

print "\n\nSecond try:"
menu()
输出:

First try:
e
c
d
a
b


Second try:
b
c
a
e
d
链接到docu以获取


我不清楚为什么在这里使用类会使代码受益

你必须澄清你不露面是什么意思?请给出一个答案。
。他们甚至没有出现。
??调用
Start().Start()
时,不会对返回值进行任何操作。感谢您的回复。就像我说的,我对编码是新手,这是我第一次真正的文本冒险。我会尽力按你说的去做。