无法使用Python 2.7.4中的类将列表随机化 [英] Can't randomize list with classes inside of it Python 2.7.4

查看:105
本文介绍了无法使用Python 2.7.4中的类将列表随机化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new to coding and I need some help. I'm trying to randomize these rooms or 'scenes' in a text adventure but whenever I try to randomize it they don't even show up when I run it! Here is the script:

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()

推荐答案

以某种方式,您会以错误的方式使用语法.您创建一个词典,其中class-实例作为,而strings作为.

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.

如果您想使用类或函数作为dict的值,那并不重要-对于类,您还必须以某种方式存储要调用的方法(或简单地打印类并编写有效的 str ()).

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:

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

链接到文档以获取 random.shuffle()

为什么在这里使用类会使您的代码受益?我还不清楚...

Why using classes here would benefit your code is not clear to me...

这篇关于无法使用Python 2.7.4中的类将列表随机化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆