[Python] Trying to write a program in python that determines how many pyramids can be built...

Discussão em 'Python' iniciado por Stack, Agosto 8, 2025.

  1. Stack

    Stack Membro Participativo

    basically if there are 3 blocks the program should say 2 layers - 2 blocks on the bottom and then 1 on the top (total 3 blocks used)

    if there are 6 bricks available then the program should say 3 layers - 3 blocks on the bottom, 2 above it, and then 1 on the top

    if there are 7 bricks available then the program should also be 3 layers - 3 blocks on the bottom, 2 above it, and then 1 on the top, 1 left over which is not enough for a new layer

    i've got it working however for some reason it acts strange when I put in 20 bricks, where it gives me a negative number and increases the layer,

    I fixed this with an if statement, but I do not understand why this is happening, could use some advice. Thanks

    blocks = int(input("Enter the number of blocks: "))
    layer = 0
    layer_count = 0

    while blocks >= layer:

    layer = layer + 1
    blocks = blocks - layer
    layer_count = layer_count + 1

    print('blocks needed for layer is', layer)
    print('blocks remaining are', blocks)
    print('number of layers completed is', layer_count)
    print()
    if blocks < 0:
    layer_count = layer_count - 1


    print('Final count is', layer_count)

    Continue reading...

Compartilhe esta Página