Posts

Showing posts from March, 2022

Solid right angle triangle 2

 Solid right angle triangle 2 n=int(input()) for i in range(n):     for j in range(i+1):         print("*",end=" ")     print() for i in range(n):     for j in range(i+1):         print("*",end=" ")     print()     

prints the product of n given numbers

 prints the product of n given numbers:     n=int(input())      l=[]      r=1      while(n>0):          l.append(int(input()))          n-=1      for i in l:          r*=i     print(r)

solid rectangle 2

  solid rectangle 2      m=int(input())      n=int(input())      while(m>0):          print("+ "*n)          m-=1

write a program to print intezers from n to 1

  write a program to print intezers from n to 1      n=int(input())      while(n>0):          print(n)          n-=1

prints each character of a word in new line

 prints each character of a word in new line      a=input()      for i in a:          print(i)

sum of given numbers

sum of given numbers        n=int(input())      c=0      l=[]      while(c<n):          l.append(int(input()))          c+=1      print(sum(l))     

read n inputs

  read n inputs      n=int(input())      c=0      l=[]      while(c<n):          l.append(int(input()))          c+=1      for i in l:          print(i)     

sum of natural numbers

 sum of natural numbers       n=int(input())      c=0      s=0      while(c<=n):          s+=c          c+=1      print(s)

print right angle triangle

print right angle triangle       n=int(input())      for i in range (1,n+1):          print("* " * i)

prints rectangle

  prints rectangle      r=int(input())      c=int(input())      s=0      while(s<r):          print("* " * c)          s+=1

solid square

 solid square      n=int(input())      c=0      while(c<n):          print("*" " "*n)          c+=1

print intezers from m to n

 print intezers from m to n      m = int(input())      n = int(input())      while m <= n:          print(m)          m = m + 1

print integers

 print integers      n=int(input())      c=1      while(c<=n):          print(c)          c+=1

Loops

Image
  Loops So far we have seen that Python executes code in a sequence and each block of code is executed once. Loops allow us to execute a block of code several times. While Loop Allows us to execute a block of code several times as long as the condition is True . While Loop Example The following code snippet prints the next three consecutive numbers after a given number. Code 1 2 3 4 5 6 a = int ( input ( )) counter = 0 while counter < 3 : a = a + 1 print ( a ) counter = counter + 1 PYTHON Input 4 Output 5 6 7 Possible Mistakes 1. Missing Initialization Code 1 2 3 4 5 6 a = int ( input ( )) while counter < 3 : a = a + 1 print ( a ) counter = counter + 1 print ( "End" ) PYTHON Input 5 Output NameError: name 'counter' is not defined 2. Incorrect Termination Condition Code 1 2 3 4 5 6 7 a = int ( input ( )) counter = 0 condition = ( counter < 3 ) while condition : a =...