需要将每个列表元素从现有列表中减少1并将其附加到新列表

时间:2014-12-06 23:43:40

标签: python-3.x

以下是我需要帮助的代码:

import random
def GetUserInput():

    numStacks = int(input("Enter an integer between 1 and 45 (inclusive): "))
    while numStacks < 1 or numStacks > 45:
        print("Number is invalid.")
        return numStacks

    createInitialStacks(numStacks)

def createInitialStacks(numStacks):       
    print("Number of stacks:", numStacks)
    stacks = []
    cards = 45

    for card in range(numStacks - 1):
        value = random.randint(1, (cards + 1) - numStacks)
        stacks.append(value)
        numStacks -= 1
        cards -= value
    stacks.append(cards + 1 - numStacks)         

    print("Stack =",stacks)

def playRound(stacks):
    stacksResult = []
    stacksResult = [x-1 for x in stacks if x > 1]
    print("New Stack =",stacksResult)

这是我的输出:

Enter an integer between 1 and 45 (inclusive): 4
Number of stacks: 4
Stack = [6, 30, 4, 5]

输出应如下所示:

Enter an integer between 1 and 45 (inclusive): 4
Number of stacks: 4
Stack = [6, 30, 4, 5]
New Stack = [5, 29, 3, 4]

此外,如果新堆栈中有任何零,则需要将其删除。

1 个答案:

答案 0 :(得分:0)

stacksResult = [x = 1表示堆叠中的x,如果x> 1]