Python code to display in a pattern given

prg11

Firstly, welcome to the forums.

While we are primarily here to help people with their Free Code Camp progress, we are open to people on other paths, too. Some of what you are asking is pretty trivial in the Free Code Camp context, so you might find that if you’re not getting the instruction and material you need in your current studies, the FCC curriculum will really help you get started. At a modest guess I’d say investing a 4-5 hours working through the curriculum here will really pay off. You can find the curriculum at https://www.freecodecamp.org/learn.

With your current questions, we don’t have enough context to know what you already know or don’t know, so it is impossible to guide you without just telling you the answer (which we won’t do).

It is pretty typical on here for people to share a codepen / repl.it / jsfiddle example of what they have tried so that anyone helping has more of an idea of what help is actually helpful.

Please provide some example of what you’ve tried and I’m sure you’ll get more help.

Happy coding :slight_smile:

Hi, thank you for your reply.

I have tried the following code but unfortunately not able to get the result as expected

num =3
for i in range(num):
        for j in range(num):
            print(j+1, end=' ')
            if j>i:break
            print('')

Can you explain your plan for how to get these numbers to appear as shown? I’m not sure I understand


I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

Hi, Thanks for guiding me on posting the code in proper format.
I have modified the code and able to print first part of output and could you please help me on the second part


num =3
p=1
for i in range(1,num+1):
    for j in range(i):
         if p>=i:
          print(p, end=' ')
          p=p+1

    print('')
    if i == num:
        pd=p
        for k in range(num):
            for l in range(1):
                if pd>k:
                 print(pd-num, end=' ')
                 pd = pd+1
        print('')



ok. Let me explain.

If the given input is 3 (num), the output should be

1
2 3
4 5 6
4 5 6
2 3
1

That is, should start with single column and finish in 3 columns and reduce from 3 columns to 1. The numbers should be as it is in output.

Hope you understand.

I understand what the output should be. I don’t understand what process you are planing on making the code follow to make that output.

The process I try to achieve is

  1. start Loop ‘i’ to maintain the columns 1 to n
  2. loop j to print the values in columns
  3. variable ‘p’ to increase the printing value

kindly refer the second code I have posted.

Second part : If condition to check whether expected columns reached and after reaching the n columns, print the same row again and trying to reduce the columns by 1 using loops.

We’re talking past each other.

I can read you code. I know what it literally says. I cannot read your brain.

Running your code, it looks like you have the first half working ok and you need to get the numbers smaller again.

I’m not quite sure how this part will get you there.

Using this code able to print the first row of second part… from there dont know how to reduce the columns

Maybe you can try thinking of this differently. I would think of this in rows instead of columns.

The last 3 rows are just the first 3 rows in reverse order. What’s the simplest possible way to do that?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.