Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

Assignment 2 Utkarsh

Uploaded by

bubunkumar84
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Assignment 2 Utkarsh

Uploaded by

bubunkumar84
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Assignment 2 - Utkarsh Gaikwad

Assignment 2 Provided by PWSkills Link below Dated 30th January 2023

Assignment PDF

Question 1

Write a Program to accept percentage from user and display grade according to following
Criteria
Marks Grade

>90 A

>80 and <=90 B

>=60 and <=80 C

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)

print(f"Marks of student are : {marks} ") #Display marks of student

"""
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('*****************')

Marks of student are : 95


Grade Obtained : A
Grade Obtained : A
*****************

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

10% >50000 and <=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)}")

Price of bike is Rupees 115000.0


Tax Applicable is 15% and Tax amount is Rupees 17250.0

Question 3

Accept any city from user and display monuments of that city
City Monument

Delhi Red Fort

Agra Taj Mahal

Jaipur Jal Mahal

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.')

City Entered is : Jaipur


Monument of Jaipur is Jal Mahal.
Question 4

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

x3 − 7 ∗ x2 2) It is asked that solve this equation with accuracy of 10− 6 3)


1) Suppose i want to solve equation :
+8∗x−3
=0 −
Here our f(x) = x3 − 7 and solve for x until |f(x)| < 10 6 4) let x0 be initial guess i.e. any random guess 5)
∗ x2 + 8 ∗ x
−3
Newton Rhapsons equation give by xn = xn 6) f ′ (x) = 3 ∗ x2
− f(xn ) − 14 ∗ x + 8
/f ′ (xn )

Link to Wikipedia for Newton Rhapson Method

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)}')

Iteration 1: xn = 7.755952380952381 , accuracy = 104.52178131917454


Iteration 2: xn = 6.447484728483863 , accuracy = 25.611785213511084
Iteration 3: xn = 5.844079172698472 , accuracy = 4.2741672359087275
Iteration 4: xn = 5.694855431215423 , accuracy = 0.2312060956792834
Iteration 5: xn = 5.685811988487806 , accuracy = 0.0008240151136575946
Iteration 6: xn = 5.685779526507159 , accuracy = 1.059829202176843e-08

Root for equation is : 5.685779526507159


Accuracy is : 1.059829202176843e-08

Question 6

Use nested while loop to print 3 different patterns

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

Reverse a while loop to display numbers from 10 to 1

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

10
9
8
7
6
5
4
3
2
1

Question 8

Reverse a while loop to display numbers from 10 to 1

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

You might also like