-->

Python 3.2.1.14 LAB: Essentials of the while loop

 3.2.1.14 LAB: Essentials of the while loop

while loop
while loop

If you're taking PCAP - Programming Essentials In Python , you may have encountered this question 3.2.1.14 LAB: Essentials of the while loop.

Objectives

Familiarize the student with:

  • using the while loop;
  • finding the proper implementation of verbally defined rules;
  • reflecting real-life situations in computer code.

Scenario

Listen to this story: a boy and his father, a computer programmer, are playing with wooden blocks. They are building a pyramid.

Their pyramid is a bit weird, as it is actually a pyramid-shaped wall - it's flat. The pyramid is stacked according to one simple principle: 

👉 each lower layer contains one block more than the layer above 👈.

The figure illustrates the rule used by the builders:

Your task is to write a program which reads the number of blocks the builders have, and outputs the height of the pyramid that can be built using these blocks.

Note: the height is measured by the number of fully completed layers - if the builders don't have a sufficient number of blocks and cannot complete the next layer, they finish their work immediately.

Test your code using the data we've provided.

Test Data

Sample input: 6

Expected output: The height of the pyramid: 3

Sample input: 20

Expected output: The height of the pyramid: 5

Sample input: 1000

Expected output: The height of the pyramid: 44

Sample input: 2

Expected output: The height of the pyramid: 1

Solution Code

Solution #1

blocks = int(input("Enter the blocks : "))
height = 0
layers = 1
while layers <= blocks:
    height += 1
    blocks -= layers
    layers += 1
print("The height of the pyramid: ", height)

Solution #2

blocks = int(input("Enter number of blocks: "))
print(f'You can build a pyramid {int(0.5 * ((8 * blocks + 1)**0.5 - 1))} blocks high')
# This is how a pyramid formula is done

Solution #3

blocks = int(input("Enter number of blocks: "))

for n in range(blocks):
    if n*(n+1)/2 <= blocks: # This is how a pyramid formula is done
        height = n

print("The height of the pyramid is:", height)


=================================================================================

Follow the blog to be the first to know ❤

Post a Comment

10 Comments

  1. B=100
    BB=0
    S=0
    while (B>=(S+1)**2):
    S=S+1
    B=B-S**2
    BB=BB+S**2
    print(S,") B=",S**2," | BB=",BB)
    print(S)

    ReplyDelete
  2. Thank you for the solution. Try to test it and match it with the requirements. Also, try to make the variables meaningful so the user can know which refers to what.

    ReplyDelete
  3. Hey thank you for your help. The problem said that the pyramid layers one block greater than the previous but when i put 4 blocks it outputs 2 layers which is a square or rectangle. can you help me?

    ReplyDelete
    Replies
    1. The problem said a pyramid layer, therefore if you insert 4 blocks, only three blocks will be useful to form a pyramid layer which is why you will have a height of two

      Delete
  4. The first solution fail when the input was 1000, the answer was 10.

    ReplyDelete
    Replies
    1. I uploaded a video for each solution and they work just fine. Check the videos and make sure you're writing the exact code. Please let me know in the comments.

      Delete
  5. Great article! I found the information you shared to be insightful and thought-provoking. The examples you provided really helped illustrate your points effectively. I appreciate the effort you put into creating this content and look forward to reading more from you in the future. Keep up the excellent work! if you need homes contact Top-rated Home Builders in Guelph

    ReplyDelete
    Replies
    1. It's amazing to hear this Rachel. You can also keep up with our python lessons on our YouTube Channel here
      https://youtube.com/playlist?list=PLAYx9j051JpluOiHnfHUSTJgm5JUgNSpX

      Delete
  6. I made my way:

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

    i = 1
    height = 0
    layers=[]
    if blocks <= 0:
    print("Blocks needs to be > 0")
    else:
    acumulative = 0
    while i <= blocks:
    if (1 in layers) == False and acumulative == 0:
    layers.append(1)
    acumulative = 0
    height = len(layers)
    else:
    acumulative += 1
    if (int(layers[-1]) + 1) == acumulative:
    layers.append((int(layers[-1]) + 1))
    acumulative = 0
    height = len(layers)
    i += 1
    print("The height of the pyramid: ", height)

    ReplyDelete

Your opinion matters, your voice makes us proud and happy. Your words are our motivation.