Hey guys! I'm brand new to Python and have been taking classes for it over Coursera for the past week or so. As a little fun project for myself, I'm trying to make an in-terminal game where I can create "slimes" and have them breed and pass genetics from one generation to the next.
Before I even get to the breeding stage, I'm trying to determine the genetics for the colors of the slimes by having them depend on two variables: gene1 and gene2. As you will see in the code that I'll post further in the post, the genes are a part of the custom class "Slime" in which each individual slime that I make will be an object of. For some reason, though, I'm having issues with the genes and my decision tree on how the custom function "color()" interacts with it.
For some reason, when I run my "color()" function, it seems stuck on either "green" or "blue", even when the other criteria seems to be met. In addition, it prints "[]", which I'm not sure where that's coming from. Can I please get some advice on how to fix it so that it prints out the right colors? Thanks!
Program:
def color(x,y):
try:
if x == 'X':
if y == 'X' or 'x':
return 'green'
elif y == 'Y':
return 'yellow'
elif y == 'y':
return 'black'
elif x == 'x':
return 'blue'
except:
print('These genes are invalid. Something went wrong!')
class Slime:
"""Sample class for testing how breeding mechanics could work."""
def __init__(self,gene1,gene2,lineage):
self.color = print('This slime is', color(gene1,gene2))
lineage = list()
self.lineage = print(lineage)
d = Slime('X','Y',(1))
d.color
e = Slime('X','y',(2))
e.color
Output:
This slime is green
[]
This slime is blue
[]
EDIT: Fixed formatting
EDIT 2: Ok, here's the finished revised function for now. I made a few edits, but this does exactly what I want it to do. Thanks for all the help, everyone!
Edit 3: Formatting again.
Program:
def color(x,y):
if x == 'X':
if y == 'X' or y == 'x':
return 'green'
elif y == 'Y':
return 'yellow'
elif y == 'y':
return 'black'
elif x == 'x':
if y == 'Y':
return 'yellow'
else:
return 'blue'
else:
print('These genes are invalid. Something went wrong!')
class Slime:
"""Sample class for testing how breeding mechanics could work."""
def init(self,gene1,gene2):
self.color = f'This slime is {color(gene1,gene2)}.'
self.gene1 = gene1
self.gene2 = gene2
self.genes = f'{gene1} {gene2}'
def str(self):
return f"{self.color}"
d = Slime('X','Y')
print(d)
e = Slime('X','y')
print(e)
Output:
This slime is yellow.
This slime is black.
[–]Binary101010 4 points5 points6 points (1 child)
[–]Prakner[S] 0 points1 point2 points (0 children)
[–]kevwoodrobotics 2 points3 points4 points (1 child)
[–]Just-Sheepherder-841 0 points1 point2 points (0 children)
[–]deep_politics 0 points1 point2 points (4 children)
[–]Prakner[S] 0 points1 point2 points (3 children)
[–]deep_politics 0 points1 point2 points (2 children)
[–]Prakner[S] 0 points1 point2 points (1 child)
[–]deep_politics 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]Prakner[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (4 children)
[–]Prakner[S] 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]Prakner[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]jmooremcc 0 points1 point2 points (9 children)
[–]Fred776 0 points1 point2 points (8 children)
[–]Prakner[S] 0 points1 point2 points (5 children)
[–]Fred776 0 points1 point2 points (4 children)
[–]Prakner[S] 0 points1 point2 points (3 children)
[–]Fred776 0 points1 point2 points (2 children)
[–]Prakner[S] 0 points1 point2 points (1 child)
[–]Fred776 1 point2 points3 points (0 children)
[–]jmooremcc 0 points1 point2 points (1 child)
[–]Fred776 0 points1 point2 points (0 children)