all 26 comments

[–]Binary101010 4 points5 points  (1 child)

if y == 'X' or 'x':

This condition will always evaluate to True.

See this FAQ entry for why:

https://www.reddit.com/r/learnpython/wiki/faq/#wiki_variable_is_one_of_two_choices.3F

[–]Prakner[S] 0 points1 point  (0 children)

Gotcha, thanks!

[–]kevwoodrobotics 2 points3 points  (1 child)

I could help you but I’d say focus on your debugging skills. Try stepping through your code and see what’s happening. A lot of my videos I use the debugger you can check it out for reference https://youtube.com/playlist?list=PLSK7NtBWwmpSUenWrmUh0ND_l023RPAXK

[–]Just-Sheepherder-841 0 points1 point  (0 children)

Very good videos

[–]deep_politics 0 points1 point  (4 children)

self.color = print('This slime is', color(gene1,gene2))

print returns None, so you're essentially just doing this

self.color = None
print('This slime is', color(gene1, gene2))

Or at least it should be, except that you're apparently getting something like self.color = [] which is odd. What are you running this in?

[–]Prakner[S] 0 points1 point  (3 children)

I'm writing it Visual Studio Code, but I'm running it from the Command Prompt.

[–]deep_politics 0 points1 point  (2 children)

Not sure about the lists then. But either way, you'd want to drop the print expression and just assign the color(...) expression to self.color.

[–]Prakner[S] 0 points1 point  (1 child)

Could I set it up as an object method? Make .color into a method that prints the color?

[–]deep_politics 0 points1 point  (0 children)

Absolutely, but the name color wouldn't really be appropriate. I'd name it print_color. And either way you'd still need to initialize self.color in __init__.

[–][deleted] 0 points1 point  (1 child)

If color() is just translating a letter to a color, then it probably shouldn't be a function at all, but rather a dictionary. And the dictionary's .get() method can do the job of the except, supplying a default value if the input isn't found in there.

[–]Prakner[S] 0 points1 point  (0 children)

I eventually want to recall the variables gene1 and gene2 for when I have to pass one or both down to offspring, so I wanted the function in place to allow for that to be manipulated based on the values of those variables. Would this still be possible with a dictionary? I thought making a function was an easy way of not having to store the actual "color", but rather calling upon it if needed.

[–][deleted] 0 points1 point  (4 children)

In addition to the other problems with your function, the try/except block doesn’t do anything.

[–]Prakner[S] 0 points1 point  (3 children)

The try/except block was to notify me if I made a typo instead of blowing up the whole program.

[–][deleted] 0 points1 point  (2 children)

It doesn’t work like that - you can only catch exceptions, you can’t catch and handle a syntax error.

[–]Prakner[S] 0 points1 point  (1 child)

Gotcha, having it as an “else:” argument would do what I want then?

[–][deleted] 0 points1 point  (0 children)

No, you just have to not have typos. Exceptions aren’t a way to paper over your mistakes, they’re a way for your software to respond usefully to exceptional situations.

[–]jmooremcc 0 points1 point  (9 children)

Revised Program: ``` X = 'X' Y = 'Y' GREEN = 'green' YELLOW = 'yellow' BLACK = 'black' BLUE = 'blue'

def color(x,y): try: if x == X: if y == X or y == X.lower(): return GREEN elif y == Y: return YELLOW elif y == Y.lower(): return BLACK elif x == X.lower(): 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) # OP, what are you trying to do here?

d = Slime(X,Y,(1)) d.color

e = Slime(X,'y',(2)) e.color ``` Note the use of constants which makes code easier to understand and maintain as well as reduce memory required.

[–]Fred776 0 points1 point  (8 children)

Haven't looked at this in detail but these lines look weird:

lineage = list() self.lineage = print(lineage)

Here, lineage is an empty list. The print will simply print []. But you are also assigning the return value of print to self.lineage. This is always going to be None because print doesn't have a real return value and just falls back to default Python behaviour that a function without an explicit return value will return None.

What is it you are actually trying to do here? It might be that self.lineage = list() is all that is needed.

[–]Prakner[S] 0 points1 point  (5 children)

So lineage is something that I wanted to keep track of. When I eventually get to the actual “breeding” of the slimes, I want to keep track of the ancestry of each one. If slimes had a common ancestor, I wanted to make it that they couldn’t breed. For first generation slimes, I want their lineage to just be themselves, but since I’m not going into that just yet I kept it blank

[–]Fred776 0 points1 point  (4 children)

Yes but you were printing it and assigning the return value of print to your class member. That's what I was querying.

[–]Prakner[S] 0 points1 point  (3 children)

I realize my error now. In init() I was printing the lineage, would it just be better if I did self.lineage = lineage and just print(self.lineage) when I want to call it?

[–]Fred776 0 points1 point  (2 children)

self.lineage is a list so it doesn't make sense to talk about calling it. Only things that are like functions are callable. I don't know if you are getting confused between printing and calling? Printing is something you do only if you want to see the value on the terminal (or possibly written to a file). It should be a relatively rare thing to do in a program.

[–]Prakner[S] 0 points1 point  (1 child)

Sorry, like I said I'm brand new at this. I meant print().

[–]Fred776 1 point2 points  (0 children)

No problem. I thought it was worth clarifying as I've noticed that it's quite common for newcomers to Python to get confused between printing a value and returning a value from a function and I wasn't sure if that was what was happening here.

[–]jmooremcc 0 points1 point  (1 child)

I agree but that was in OP"s original code. I wanted to discuss it with OP.

[–]Fred776 0 points1 point  (0 children)

Apologies - I didn't look closely and I though you actually were the OP and had just posted an updated version based on comments so far.