Click here to Skip to main content
15,867,979 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
 Print the following pattern in python for the given number of rows.
*
  *
    *
  *
*
  *
    *
  *
*
  *

i am not getting * at the row 6 and 10 


What I have tried:

Python
for i in range(10):
    for j in range(3):
        if (i + j) % 4 == 0 or (i == j):
            print("*", end=" ")
        else:
            print(" ", end=" ")
    print()
Posted
Updated 17-Jul-23 7:42am
v3
Comments
Patrice T 10-May-23 5:50am    
Show your work so far and explain problem you have.
Smoker_Avi 10-May-23 5:54am    
I am not getting the logic how to build, can you solve
Patrice T 10-May-23 6:07am    
The whole purpose of this problem is that you figure out the logic, or at least part of it.
If you are blank, have a talk with your teacher.
Smoker_Avi 10-May-23 9:42am    
for i in range(10):
for j in range(3):
if (i == j):
print("*", end=" ")
else:
print(" ", end=" ")
print()

here my code can you suggest when get the loop
Patrice T 10-May-23 9:56am    
Use Improve question to update your question.
So that everyone can pay attention to this information.

Let's have a variable r that counts the rows, starting from zero, so that
at first row r = 0
at second row r = 1
...

Now let's have another variable c that contains the column of the '*', so that
at 1st row r = 0, c = 0
at 2nd row r = 1, c = 1
at 3rd row r = 2, c = 2
at 4th row r = 3, c = 1
at 5th row r = 4, c = 0
at 6th row r = 5, c = 1
at 7th row r = 6, c = 2
...

Do you spot the pattern ? :-D
Is there a relation between the value of r and the one of c ?
 
Share this answer
 
Comments
Patrice T 10-May-23 19:03pm    
+5
CPallini 11-May-23 1:10am    
Thank you.
Quote:
How can I write this pattern in Python

First of all, your code is too complicated!
No matter what, each output line ends with a '*'
This makes this code:
Python
for i in range(10):
    # here you just need to tell how many spaces
    Spaces= i
    for j in range(Spaces):
        print(" ", end=" ")
    print("*", end=" ")
    print()

After that, you need to get the way to tell how many spaces you want.
Partial solutions:
Python
Spaces= i % 3

Python
Spaces= 5- i % 5

Study the abs() function
 
Share this answer
 
v2
While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]

[edit]
If it helps, this is a simple problem: I just thought for a minute and then wrote the code in C# - it needed three lines of code once you have the input value, is all. So in Python, it should be pretty easy. Think about what you are being asked to do and it should be pretty obvious.
[/edit]
 
Share this answer
 
v2
Hello this is Gulshan Negi
Well, you need to run below code what you are looking for.

for i in range(11):
    for j in range(3):
        if (i + j) % 4 == 0 or (i + j) % 4 == 3:
            print("*", end=" ")
        else:
            print(" ", end=" ")
    print()


Thanks
 
Share this answer
 
Comments
Dave Kreskowiak 17-Jul-23 13:52pm    
Doing homework for someone is never a good idea.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900