Reputation: 3535
I want to know, how can I test for a function randomly pulled from a list in a conditional statement? Here is some example code. Just ignore what the code is supposed to be printing.
import random, time
def biomeLand():
print "Biome: Land"
def biomeOcean():
print "Biome: Ocean"
def biomeDesert():
print "Biome: Desert"
def biomeForest():
print "Biome: Forest"
def biomeRiver():
print "Biome: River"
biomes = [biomeLand, biomeOcean, biomeDesert, biomeForest,
biomeRiver]
def run():
while True:
selected_biome = random.choice(biomes)()
time.sleep(0.5)
run()
Once again how can I make it so the program tests in a conditional statement when a certain function is called from the list?
Upvotes: 1
Views: 68
Reputation: 877
You can just match them like any other variable:
def foo():
print "foo"
def bar():
print "bar"
first = foo
print (first == bar) # prints "False"
print (first == foo) # prints "True"
So in your example you can just have something like:
if selected_biome == biomeLand:
# do something
Upvotes: 1
Reputation: 398
maybe:
def run():
while True:
selected_biome = random.choice(biomes)
selected_biome()
if selected_biome == biomeLand:
print "biomeLand Selected"
time.sleep(0.5)
run()
Upvotes: 2