2

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 = yes

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???

0

4 Answers 4

5

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
7
  • But in the one I am coding it's not just wether is swims or not, It is a list of random questions with multiple choice answers... how would I impliment the different answers into this? Commented Apr 27, 2011 at 21:32
  • @Blender This is great, thank-you so much! But how would I let the person choose the answer? Commented Apr 28, 2011 at 21:47
  • print the_question.choices to show answers, and do a raw_input() to catch user input.
    – Blender
    Commented Apr 29, 2011 at 19:42
  • @Blender Hasn't worked. I says syntax error and highlights 'the_question' Commented Apr 29, 2011 at 19:52
  • I meant do that to a question object, where the_question is the question ;) Try print test.questions[0].choices.
    – Blender
    Commented Apr 29, 2011 at 20:00
0

You need to check what animal is with an if statement:

if animal == 'bird':
    flight = yes
    swim = no

and so on.

0

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

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.