我需要帮助让我的条件语句使用For循环查看数组值。你知道吗
我的印象是,这可能是因为我没有像在其他两个if语句中那样使用b'X',但我就是想不出正确的语法来实现这一点,而且我甚至不知道b在那里做什么。你知道吗
R和X击键确实正确地执行了它们的代码,但是作为FOR循环的一部分被检查的p、N、D和Q却不正确。你知道吗
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
对于更多上下文,the entire project是GitHub上的开源。你知道吗
我认为你的代码应该是正确的。但这取决于
GetKey()
的输入。你知道吗我在底部添加了以下几行:
我得到了这个结果:
另一方面,如果将字节而不是字符串传递给
GetKey()
,如下所示:你只会看到这一行重复:
这是因为
Token
中的硬币代码是字符串'P' 'N' 'D' 'Q'
。如果将它们与b'P' b'N' b'D' b'Q'
进行相等比较,结果将是False
,因此需要将Token
更改为如下所示:我想有一个很好的理由,像硬件一样,使用字节而不是字符串。你知道吗
相关问题 更多 >
编程相关推荐