0

I need help getting my conditional statement to look through array values using a For Loop.

I have the impression that it may because I'm not using b'X' as I am in the other two if statements, but I just can't figure out the right syntax to get this going, and I'm not even sure what the b is doing there anyway.

The R and X keystrokes do execute their code properly, but P, N, D, and Q which are checked as part of the FOR loop, do not.

Token = [['P',0,.01,"Penny"],['N',0,.05,"Nickel"],['D',0,.10,"Dime"],['Q',0,.25,"Quarter"]]

def GetKey(CoinIn): # Recieve a coin, update all total counts and values
    if CoinIn == b'R':     # Reset All Values and counts to 0
        for i in Token:
            i[1] = 0
    elif CoinIn == b'X':   # Exit Request
        return('X')
    else:                 # HERE IS WHERE THE CODE BREAKS
        for i in Token:
            if CoinIn == i[0]:
                i[1] += 1

For more context, the entire project is open source on GitHub.

NickSentowski
  • 820
  • 13
  • 27
  • 1
    What do you mean by "get this going"? What exactly is not working and what is the desired behaviour? It's not clear to me how this should work. – roganjosh Aug 07 '17 at 19:11
  • I'll edit the question... Basically, the R and X keystrokes execute the appropriate code, but P, N, D, and Q, do not. – NickSentowski Aug 07 '17 at 19:13
  • But why should P, N, D and Q do anything at all since there's no specified behaviour for them in your code? Also see https://stackoverflow.com/questions/6269765/what-does-the-b-character-do-in-front-of-a-string-literal – roganjosh Aug 07 '17 at 19:14
  • If CoinIn is not X or B, it should go to else, which does a For-Loop check against the first character in each element of Token. When it matches, The 2nd element +=1. Is this not a good way of attempting this? – NickSentowski Aug 07 '17 at 19:17
  • As far as I can tell, this code works as expected, as noted by @BoarGules. If you haven't printed the output at any point as was suggested, then I'm curious as to what made you think this wasn't working. – roganjosh Aug 07 '17 at 19:25

1 Answers1

1

I think your code works exactly as it should. But that depends on the input to GetKey().

I added the following lines at the bottom:

GetKey('P')
GetKey('P')
print(Token)

GetKey('N')
GetKey('D')
print(Token)

GetKey('Q')
print(Token)

And I got this output:

[['P', 2, 0.01, 'Penny'], ['N', 0, 0.05, 'Nickel'], ['D', 0, 0.1, 'Dime'], ['Q', 0, 0.25, 'Quarter']]
[['P', 2, 0.01, 'Penny'], ['N', 1, 0.05, 'Nickel'], ['D', 1, 0.1, 'Dime'], ['Q', 0, 0.25, 'Quarter']]
[['P', 2, 0.01, 'Penny'], ['N', 1, 0.05, 'Nickel'], ['D', 1, 0.1, 'Dime'], ['Q', 1, 0.25, 'Quarter']]

If, on the other hand, you pass bytes not strings to GetKey(), like this:

GetKey(b'P')
GetKey(b'P')
print(Token)

GetKey(b'N')
GetKey(b'D')
print(Token)

GetKey(b'Q')
print(Token)

you will just see this line repeated:

[['P', 0, 0.01, 'Penny'], ['N', 0, 0.05, 'Nickel'], ['D', 0, 0.1, 'Dime'], ['Q', 0, 0.25, 'Quarter']]

That is because the coin codes in Token are the strings 'P' 'N' 'D' 'Q'. If you compare them for equality with b'P' b'N' b'D' b'Q' the result will be False, so you need to change Token to look like this:

Token = [[b'P',0,.01,"Penny"],[b'N',0,.05,"Nickel"],[b'D',0,.10,"Dime"],[b'Q',0,.25,"Quarter"]]

I suppose there is a good reason, like hardware, for working with bytes instead of strings.

BoarGules
  • 16,440
  • 2
  • 27
  • 44
  • Thank you, this is a debug I haven't tried yet. I will examine further. – NickSentowski Aug 07 '17 at 19:21
  • Your edit has completely solved my issue. To be honest, I'm not sure that I had a great reason to use bytes instead of strings... It was recommended to me to solve an earlier issue, I think regarding case-sensitivity. As a rather amateur programmer myself, I tend to trust anyone on SO with a higher rep than me, and figure I'll learn my lesson later. – NickSentowski Aug 08 '17 at 15:04