I wan an array which is going to have about 30 things in it. Each thing in the array is going to be a set of variables, and depending on which thing in the array is chosen, different variables will be set.
e.g.
foo = ['fish', 'mammal', 'bird']
ranfoo = random.randint(0,2)
animal = foo[ranfoo]
this works fine for returning a random element from the list, but how then, depending on the item selected do I assign some variables to them?.
e.g. 'bird' has been randomly selected, I want to assign : flight = yes swim = no. Or something along those lines... What I'm programming is a little more complicated but that's basically it. I've tried this:
def thing(fish):
flight = no
swim = ye开发者_C百科s
def thing(mammal):
flight = no
swim = yes
def thing(bird):
flight = yes
swim = no
foo = ['fish', 'mammal', 'bird']
ranfoo = random.randint(0,2)
animal = foo[ranfoo]
thing(animal)
But that doesn't work either and I don't know what else to do... Help???
How about making a thing
class?
class thing:
def __init__(self, type = ''):
self.type = type
self.flight = (self.type in ['bird'])
self.swim = (self.type in ['fish', 'mammal'])
Now, it's quite simple to choose a random "thing":
import random
things = ['fish', 'mammal', 'bird']
randomThing = thing(random.sample(things, 1))
print randomThing.type
print randomThing.flight
print randomThing.swim
So you are making a multiple-choice thing?
Maybe this would work:
class Question:
def __init__(self, question = '', choices = [], correct = None, answer = None):
self.question = question
self.choices = choices
self.correct = correct
def answer(self, answer):
self.answer = answer
def grade(self):
return self.answer == self.correct
class Test:
def __init__(self, questions):
self.questions = questions
def result(self):
return sum([question.grade() for question in self.questions])
def total(self):
return len(self.questions)
def percentage(self):
return 100.0 * float(self.result()) / float(self.total())
So a sample test would be like this:
questions = [Question('What is 0 + 0?', [0, 1, 2, 3], 0),
Question('What is 1 + 1?', [0, 1, 2, 3], 2)]
test = Test(questions)
test.questions[0].answer(3) # Answers with the fourth item in answers, not three.
test.questions[1].answer(2)
print test.percentage()
# Prints 50.0
You need to check what animal is with an if statement:
if animal == 'bird':
flight = yes
swim = no
and so on.
Instead of storing strings in the string, store an object that inherits from a common animal base class, then you can do:
class animal:
def thing(self):
raise NotImplementedError( "Should have implemented this" )
class fish(animal):
def thing(self):
""" do something with the fish """
self.flight = yes
self.swim = no
foo = [aFish, aMammal, aBird]
ranfoo = random.randint(0,2)
animal = foo[ranfoo]
animal.thing()
An extension of @Blender's answer:
class Thing(object):
def __init__(self, name, flies=False, swims=False):
self.name = name
self.flies = flies
self.swims = swims
foo = [
Thing('fish', swims=True),
Thing('bat', flies=True),
Thing('bird', flies=True)
]
精彩评论