mardi 3 novembre 2015

Python (Must use Nested Loops)- need help think i have it mostly figured out but not 100% sure

So I have to use python and use nested loops for the question. I have to create 4 patterns that first display them separately below one another and I think I have it figured out through looking through some help on here.

Pattern A 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6

Pattern B 1 2 3 4 5 6 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1

Pattern C 1 2 1 3 2 1 4 3 2 1 5 4 3 2 1 6 5 4 3 2 1

Pattern D 1 2 3 4 5 6 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1

And The Second part of the question says i need to be able to print them all off so that there side by side not below one another all showing at the same time

so: Pattern A Pattern B Pattern C Pattern D 1 1 2 3 4 5 6 1 1 2 3 4 5 6 1 2 1 2 3 4 5 2 1 1 2 3 4 5

etc..

Here is my code for part A so far and it seems to work, and is this not nested statements

print ("Pattern A")
for i in range(6):
    print(*range(1, i+2), sep='  ', end='\n\n')

print ("Pattern B")
for i in range(6, 0, -1):
    print(*range(1, i+1), sep='  ', end='\n\n')

print ("Pattern C")
for i in range(6):
    print('   '*(5-i), end='')
    print(*range(i+1, 0, -1), sep='  ', end='\n\n')

print ("Pattern D")
for i in range(6, 0, -1):
    print('   '*(6-i), end='')
    print(*range(1, i+1), sep='  ', end='\n\n')

Aucun commentaire:

Enregistrer un commentaire