Assignment 2 Utkarsh
Assignment 2 Utkarsh
Assignment PDF
Question 1
Write a Program to accept percentage from user and display grade according to following
Criteria
Marks Grade
>90 A
below 60 D
I have used try, except and finally just for error handling purposes
In [21]:
marks = input('Enter Marks of Student : ') #Input Marks of Student
try:
if marks.isnumeric()==True: #Checks if Numeric value entered by user and then convert
s into float
marks = int(marks)
"""
Below is main Conditions for code
"""
if marks>90:
print('Grade Obtained : A')
elif (marks>80) and (marks<=90):
print('Grade Obtained : B')
elif (marks>=60) and (marks<=80):
print('Grade Obtained : C')
else:
print('Grade Obtained : D')
except:
if marks.isnumeric()==False: #Raises error if numeric value not entered by user
raise Exception('Please enter Numeric value in Marks Characters and string not al
lowed')
finally :
print('*****************')
Question 2
Write a program to accept cost price of bike and display the road tax to be paid according
to following criteria
Tax Cost Price (in Rs.)
15% >100000
5% <=50000
In [25]:
price = float(input('Enter Price of the Bike in Rupees : '))
print(f"Price of bike is Rupees {price} ")
if price>100000:
print(f"Tax Applicable is 15% and Tax amount is Rupees {round(0.15*price,2)}")
elif (price>50000) and (price<=100000):
print(f"Tax Applicable is 10% and Tax amount is Rupees {round(0.10*price,2)}")
else:
print(f"Tax Applicable is 5% and Tax amount is Rupees {round(0.05*price,2)}")
Question 3
Accept any city from user and display monuments of that city
City Monument
In [30]:
city = input('Enter City Name : ')
print('City Entered is :',city)
#In below code i have used .lower() for case unification and then checking
if city.lower()=='delhi':
print(f'Monument of {city} is Red Fort.')
elif city.lower()=='agra':
print(f'Monument of {city} is Taj Mahal.')
elif city.lower()=='jaipur':
print(f'Monument of {city} is Jal Mahal.')
else:
print(f'City {city} was entered please enter Delhi, Agra or Jaipur.')
Check How many times a given number can be divided by 3 before it is less than equal to
10
In [48]:
num = 3030
counter = 0
while num >10:
num = num/3
print(num)
counter = counter+1
print(f'Times the Number can be divided by Three : {counter} times.')
1010.0
336.6666666666667
112.22222222222223
37.40740740740741
12.469135802469138
4.156378600823046
Times the Number can be divided by Three : 6 times.
Question 5
Why and When to use While loop in python give a detailed description with example
While Loop can be used to solve non linear equations with Newton Rhapsons Method for given decimal
accuracy
In [58]:
def f(x):
"""
Defining equation to solve
"""
return (x**3 - 7*(x**2)+ 8*x -3)
In [59]:
def der_f(x):
"""
Derrivative of f(x)
"""
return (3*x**2 - 14*x + 8)
In [81]:
# Solving Equation with Newton Rhapson method with accuracy of 10^-6
xn = 10 #Initial Guess of 10
counter = 1
while abs(f(xn))>1e-6:
xn = xn - f(xn)/der_f(xn)
print(f'Iteration {counter}: xn = {xn} , accuracy = {f(xn)}')
counter = counter + 1
print(f'\n Root for equation is : {xn}')
print(f'Accuracy is : {f(xn)}')
Question 6
In [83]:
# Pattern 1: Right Angled Star pattern
i = 1
while i <= 10:
j = 1
while j <= i:
print("*", end="")
j += 1
print("")
i += 1
*
**
***
****
*****
******
*******
********
*********
**********
In [84]:
# Pattern 2: Isoceles Triangle Star pattern
n = 10
i = 1
while i <= n:
spaces = n - i
while spaces > 0:
print(" ", end="")
spaces -= 1
stars = 2 * i - 1
while stars > 0:
print("*", end="")
stars -= 1
print("")
i += 1
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
In [85]:
#Pattern 3: Diamond
n = 5
i = 1
while i <= n:
spaces = n - i
while spaces > 0:
print(" ", end="")
spaces -= 1
stars = 2 * i - 1
while stars > 0:
print("*", end="")
stars -= 1
print("")
i += 1
i = n - 1
while i >= 1:
spaces = n - i
while spaces > 0:
print(" ", end="")
spaces -= 1
stars = 2 * i - 1
while stars > 0:
print("*", end="")
stars -= 1
print("")
i -= 1
*
***
*****
*******
*********
*******
*****
***
*
Question 7
In [88]:
i = 10
while i>=1:
print(i)
i = i - 1
10
9
8
7
6
5
4
3
2
1
Question 8
In [90]:
counter = 10
while True:
print(counter)
counter = counter-1
if counter == 0:
break
10
9
8
7
6
5
4
3
2
1