我想在我的数组元素中使用int。 现在我的代码是
Error:No resource found that matches the given name (at 'icon' with value '@mipmap/ico').
Error:No resource found that matches the given name (at 'icon' with value '@mipmap/ico').
Error:Execution failed for task ':app:processDebugResources'.
> com.android.ide.common.process.ProcessException: Failed to execute aapt
在我的代码中,我使用chr方法代替def hanoi_pos(discs, index):
power = 2**discs
stacks = ['', '', '']
for disc in range(discs):
stack = (index+power//2)//power % 3
if disc % 2 == 0: stack = (3 - stack) % 3
power = power // 2
stacks[stack] += chr(64 + discs - disc)
return stacks
def get_game_state(stacks):
return '\n'.join([' '.join(st) if st else '-' for st in stacks])
x = hanoi_pos(4, 6)
y = get_game_state(x)
print(y)
。
但是这一次,我想使用int 1,2,3····。
" A"对应于1," B"对应于2," C"对应3····。
我像chr(64 + discs - disc)
一样写了这个地方,但错误发生了。我认为声明可以使用,但它是多余的。那么,我该怎么做呢?
如何将str转换为int?
答案 0 :(得分:0)
你好mikimiki,
请尝试以下代码,
def hanoi_pos(discs, index):
power = 2**discs
stacks = ['', '', '']
for disc in range(discs):
stack = (index+power//2)//power % 3
if disc % 2 == 0: stack = (3 - stack) % 3
power = power // 2
print "--",discs, disc,"--"
stacks[stack] += chr(48 + discs - disc)
return stacks
def get_game_state(stacks):
return '\n'.join([' '.join(st) if st else '-' for st in stacks])
x = hanoi_pos(4, 6)
y = get_game_state(x)
print(y)
我希望我的回答很有帮助 如果有任何疑问请评论。