我有一个函数,它为给定的参数返回一个8位数长的二进制字符串:def rule(x):
rule = bin(x)[2:].zfill(8)
return rule
我想遍历这个字符串的每个索引并检查它是0还是1。我试着写一个这样的代码:
^{pr2}$
这个代码行不通。我得到“错误:对象不可订阅”。我要做的是编写一个接受以下输入的函数,例如:Input: 30
1. Converts to '00011110' (So far, so good)..
2. Checks if rule(30)[i] is '0' or '1' ('0' in this case where i = 0)
3. Then puts the result in a key value pair, where the index of the
string is the key and the state (on
or off) is the value.
4. The end result would be 'ruleList', where print ruleList
would yield something like this:
{0:'Off',1:'Off',2:'Off',3:'On',4:'On',5:'On',6:'On',7:'Off'}
有人能帮我吗?我不熟悉python和编程,所以这个函数已经被证明是非常有挑战性的。我希望看到一些更有经验的程序员解决这个问题。在
谢谢