RandomPRSN
RandomPRSN

Reputation: 35

Nested Loops to create a pattern

How could I use Nested Loops to create the following pattern?

111111
11111
1111
111
11
1

So far i have this and i seem to be stuck.

def main():
stars = "******"
for x in range (1,7):
    print(stars)

    for y in range (1,1):
        stars = stars.replace("*"," ")
main()

Upvotes: 1

Views: 4717

Answers (6)

Hardik Patel
Hardik Patel

Reputation: 493

def main(symbol, number):
    for x in reversed(range(number)):
        s = ""
        for y in range(x+1):
            s += symbol
        print s

main("1", 6)

You can give arguments one symbol (Example - '1','*') and number (Example - '5 for *****' to start with)

Upvotes: 1

swenzel
swenzel

Reputation: 7173

Since you're requesting a nested loop, I guess this is just a training exercise and it is not important how efficient it is.
Therefore let me propose a solution with a 'proper' inner loop and without reversed ranges:

def triangle(x, c='1'):
    for i in range(x):
        line = ''
        for _ in range(i, x):
            line += c
        print(line)

Upvotes: 0

Vivek Sable
Vivek Sable

Reputation: 10223

Check answer of Padraic Cunningham with Nested Loop.

Without Nested Loop:

  1. Create Count list by range method and reverse method of list.
  2. Iterate list and print 1 multiple of count.

code:

counters = range(1, 7)
counters.reverse()
for i in counters:
    print "1"*i

Output:

111111
11111
1111
111
11
1

Upvotes: 0

Padraic Cunningham
Padraic Cunningham

Reputation: 180411

You need to replace just 1 star in the inner loop:

stars = "******"
for x in range(6): 
    stars = stars.replace("*","1")
    print(stars)
    for y in range(1): # need range(1) to loop exactly once
        stars = stars.replace("1","",1) 

Output:

111111
11111
1111
111
11
1

If you actually want stars:

stars = "******"
for x in range(6):
    print(stars)
    for y in range(1):
        stars = stars.replace("*","",1)

Output:

******
*****
****
***
**
*

The last arg to str.replace is count where only the first count occurrences are replaced. So each time we only replace a single character.

If you have to uses the stars variable and replace then the code above will work, if you just need nested loops and to create the pattern, you can loop down from 5 and use end="" printing once in the inner loop:

for x in range(5, -1, -1):
    print("1" * x, end="")
    for y in range(1):
        print("1")

Again the same output:

111111
11111
1111
111
11
1

Upvotes: 2

ZdaR
ZdaR

Reputation: 22954

You can get the same output using a simple approach, as Python supports * operator on Strings which returns a string with repeated occurrences.

character = "1"   #You can change it to "*"
for i in range(6, 0, -1):
    print character*i

Output:

111111
11111
1111
111
11
1

Upvotes: -1

Jur Clerkx
Jur Clerkx

Reputation: 698

You are replacing all the stars in the string with the replace method, meaning you would online print one line of start. You could use the substring method for a better result.

Upvotes: 0

Related Questions