开发者社区> 问答> 正文

我正在尝试用Python制作“河内之塔”,但是我在列表中“移动”项目的方法似乎不起作用

这是我正在使用的(相关)代码。

它基本上要做的是获取塔,然后(假设)将数字移到正确的塔上。

        movefr = input("What tower do you want to move the last block from? " + str(nums) + " ")
        movef = int(movefr)

        moveto = input("What tower do you want to move the block from tower" + movefr + " to? " + str(nums) + " ")
        movet = str(moveto)

        #move numbers
        if movef == 1:
            if movet == 2:
                tower2.append(tower1[-1])
                tower1.remove(tower1[-1])
            elif movet == 3:
                tower3.append(tower1[-1])
                tower1.remove(tower1[-1])
        elif movef == 2:
            if movet == 1:
                tower1.append(tower2[-1])
                tower2.remove(tower2[-1])
            elif movet == 3:
                tower3.append(tower2[-1])
                tower2.remove(tower2[-1])
        elif movef == 3:
            if movet == 1:
                tower1.append(tower3[-1])
                tower3.remove(tower3[-1])
            elif movet == 2:
                tower2.append(tower3[-1])
                tower3.remove(tower3[-1])

它输出如下内容:

Tower1 = [5, 4, 3, 2, 1, 0]
Tower2 = []
Tower3 = []
What tower do you want to move the last block from? [1] 1
What tower do you want to move the block from tower1 to? [2, 3] 2
Tower1 = [5, 4, 3, 2, 1, 0]
Tower2 = []
Tower3 = []

有谁知道热解决这个问题?我已经尝试了所有我能想到的。

问题来源:stackoverflow

展开
收起
is大龙 2020-03-24 23:52:05 457 0
1 条回答
写回答
取消 提交回答
  • 您的movet是字符串类型:

    movet = str(moveto)
    

    更改为:

    movet = int(moveto)
    

    回答来源:stackoverflow

    2020-03-24 23:52:13
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载