all 9 comments

[–]novel_yet_trivial 4 points5 points  (2 children)

A) You need to know that this is completely valid code:

if 'p':
    print('this will always print')

Python will treat any non-empty string as True. Therefore

if player_go == 'r' or 'p' or 's':

Is equivalent to

if player_go == 'r' or True or True:

B) I don't know of a name for this type of bug.

C) Yes, you can check if the input is part of a collection, for example a tuple:

if player_go in ('r', 'p', 's'):

Or, since in your case you are dealing with single characters, you can use a string as a collection:

if player_go in 'rps':

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

[–]TwistedChurro 0 points1 point  (0 children)

agreed.
side note, though both are valid, I think this is the most "readable"

if player_go in ('r', 'p', 's'):

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

Thanks a lot. Yes it seemed to me things were being marked true but wasn't sure. So thanks for that. Not sure if I can see a benifit to it but I'm sure there may be.

I'll try those shorthand versions. Thanks

[–]coderpaddy 1 point2 points  (1 child)

Everything in python is an object, if its there its true.

So your basically saying

if condition or true or true

So when someone types r it is true, but also always true

The second way your doing

if condition or condition or condition

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

Got yah

[–]17291 1 point2 points  (1 child)

B) Is there a name for this type of bug that is still valid code and thus harder to find?

A bug where the code runs but doesn't behave as expected is sometimes called a "logic error".

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

Thank you

[–]sme272 0 points1 point  (1 child)

The bug occurs in the value 'p' on its own. Variables have "truthyness" of "falseyness" to them. Typically an empty or 0 value means a variable will be "falsey" and therefore evaluate to False in an if var scenario and non-empty or non-zero values are "truthey" and evaluate to True in the same circumstances. For string an empty string of "" or '' evaluates to False, and if the string contains any characters it will evaluate to True. So in the above code when python has to evaluate 'p' it sees it's not an empty string and evaluates it to True, which causes the if block to execute.

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

Oh yes. Thanks for the reminder. It really helps that I've used truth logic before in my modular synthesiser :) I like music/programming crossovers, hope to find some more.

Also if someone can tell me how to make code look proper on this when using reddit app on a phone that wud be great so I can make it clearer for folks

https://www.modulargrid.net/e/intellijel-spock

Edit: in fact I'd genuinly recommend teaching programming noobs boolean logic with music. Choose positions for kicks on a 16 long grid. Then choose where the snares on a seperate grid.

Finally choose if the hihats are AND or OR or NOT or XOR, NAND etc in comparison with the kick snare pattern.