Python Control Flow: Understanding if-elif-else Statements and Loops

Q1. Write a program to accept percentage from the user and display the grade according to the following criteria:

Marks - Grade
>90 - A
>80 and <=90 - B
>=60 and <=80 - C
below 60 - D

mark=int(input("Enter the percentage here"))
if mark>90:
    grade="A"
elif mark>80 and mark<=90:
    grade="B"
elif mark>=60 and mark<=80:
    grade="C"
else:
    grade="D"

print(f"Your Grade is {grade}")

Your Grade is C

Q2. Write a program to accept the cost price of a bike and display the road tax to be paid according to the following criteria:

Tax - Cost Princ (in Rs)
15% - >100000
10% - >50000 and <=100000
5% - <=50000

Bike_cost=int(input("Enter the Bike price here"))
if Bike_cost>100000:
    tax=Bike_cost*0.15
elif Bike_cost>50000 and Bike_cost<=100000:
    tax=Bike_cost*0.1
else:
    tax=Bike_cost*0.05
print(f"Road tax is : {tax}")

Road tax is : 114000.0

Q3. Accept any city from the user and display monuments of that city.

City - Monument
Delhi - Red Fort
Agra - Taj Mahal
Jaipur - Jal Mahal

city=input("Enter the city name :").lower()
if city=="delhi":
    monu="Red Fort"
elif city=="agra":
    monu="Taj Mahal"
elif city=="Jaipur":
    monu="Jal Mahul"
else:
    monu="Date not found"
print(f"Road tax is : {monu}")

Road tax is : Taj Mahal


Q4. Check how many times a given number can be divided by 3 before it is less than or equal to 10.

number=int(input("enter the number:"))
result=number//3
if number<=10:
    print("number is less than and equal to 10")
else:
    print("number is more than 10")
print(f"{number} divided by 3: {result} times")

the number is more than 10


44 divided by 3: 14 times


Q5. Why and when to use while Loop in Python give a detailed description with example.

Ans. While Loop in python execute a block of code until the given condition remains true

it used Whenever you do not know in advance how many times block of code or task required repeation

Eg.

In [9]:
sum=0
i=0
while i>=0:
    sum=sum+i
    print(f"Sum of the numbers is: {sum}")
    i=int(input("Enter the number (exit on negetive number)"))

Sum of the numbers is: 0


Sum of the numbers is: 1


Sum of the numbers is: 3


Sum of the numbers is: 7


Q6. Use nested while loop to print 3 different pattern.

In [9]:
s=0
i=0
while i < 3:
    j=10
    while j>2:
        print(" "*s,end="")
        print("*"*j)
        j=j-2
        s=s+1
    i=i+1
    

**********
 ********
  ******
   ****
    **********
     ********
      ******
       ****
        **********
         ********
          ******
           ****
In [16]:
i=0
while i < 2:
    j=20
    s=0
    while j>1:
        print(" "*s,end="")
        print("*"*j)
        j=j-2
        s=s+1
    i=i+1
    

********************
 ******************
  ****************
   **************
    ************
     **********
      ********
       ******
        ****
         **
********************
 ******************
  ****************
   **************
    ************
     **********
      ********
       ******
        ****
         **
In [36]:
import math
i=0
while i < 2:
    j=0
    s=0
    while j<20:
        print(" "*s,end="")
        print("@","*"*j,"@")
        j=j+2
        s=s+1
    i=i+1
    

@  @
 @ ** @
  @ **** @
   @ ****** @
    @ ******** @
     @ ********** @
      @ ************ @
       @ ************** @
        @ **************** @
         @ ****************** @
@  @
 @ ** @
  @ **** @
   @ ****** @
    @ ******** @
     @ ********** @
      @ ************ @
       @ ************** @
        @ **************** @
         @ ****************** @
In [38]:
i=0
while i < 2:
    j=0
    s=10
    while j<20:
        print(" "*s,end="")
        print("@","*"*j,"@")
        j=j+2
        s=s-1
    i=i+1
    

          @  @
         @ ** @
        @ **** @
       @ ****** @
      @ ******** @
     @ ********** @
    @ ************ @
   @ ************** @
  @ **************** @
 @ ****************** @
          @  @
         @ ** @
        @ **** @
       @ ****** @
      @ ******** @
     @ ********** @
    @ ************ @
   @ ************** @
  @ **************** @
 @ ****************** @

Q7. Reverse a while loop to display numbers from 10 to 1.

In [61]:
n=10
i=0
while i <10:
    print(n-i)
    i+=1
    

10
9
8
7
6
5
4
3
2
1

Q8. Reverse a while loop to display numbers from 10 to 1.

In [2]:
i=10
while i>0:
    print(i)
    i-=1

10
9
8
7
6
5
4
3
2
1
Next Post Previous Post
No Comment
Add Comment
comment url