Monday, 29 July 2019

Nested loops


Ex: wap to display *’s in pyramid style or triangle shape
n=int(input("enter no of rows "))
for i in range(1,n+1):
    print(" "*(n-i),end="")
    print("* "*i)
"""output:
enter no of rows 3
  *
 * *
* * *

Nested Loops:

Loop inside a loop is called Nested Loops.

Nested Loops:
for i in range(2):
    for j in range(2):
        print(i,j)
"""output:
0 0
0 1
1 0
1 1
>>> """
for i in range(1,2):
    for j in range(2):
        print(i,j)
output:
1 0
1 1
>>> 

#find out the output:


n=int(input("enter no of rows:"))
for i in range(1,n+1):
    for j in range(1,n+1):
        print("*",end=" ")
    print()

#find out the output:
n=int(input("enter no of rows:"))
for i in range(1,n+1):
    print("* " *i)
enter no of rows:3
*
* *
* * *
enter no of rows:3
*
* *
* * *



Next Topic: Unconditional statements
Previous Topic: while loop


1 comment:

Files with Exception handling

#Ask the user to take two input integer values var a,b try:     a=int(input("enter any integer value:"))     b=int(input(...