01/08/2024, 09:47 Python Assignment_6_Try_Except
Try-Except for condition
In [4]: # wap ask the user enter a number
# find it is a even number or odd number
# idea: any number divide by 2 , the remiander=0
# it is called as even number
try:
number = eval(input('Enter a number :'))
if number % 2 ==0 :
print (f'{number} Number is evan')
else:
print(f'{number} Number is odd')
except Exception as e:
print(e)
5.5 Number is odd
In [5]: # Implement the above problem by taking a random input between 1, 100
try:
import random
number = random.randint(1,100)
if number % 2 ==0 :
print (f'{number} Number is evan')
else:
print(f'{number} Number is odd')
except Exception as e:
print(e)
55 Number is odd
In [6]: # wap ask the user enter the distance
# if distance greater than 25km
# then enter the charge
# print the total cost
#otherwise
# print free ride
try:
distance =eval(input('enter distance :'))
amount = eval(input('enter amount :'))
if distance >= 26:
price = (distance-25)*amount
print(f'total distance is {distance} km and price is {price} Rs.')
else:
print('Free no cost')
except Exception as e:
print(e)
Free no cost
file:///C:/Users/Shubham/Downloads/Python Assignment_6_Try_Except.html 1/4
01/08/2024, 09:47 Python Assignment_6_Try_Except
In [7]: # wap ask the user enter the distance
# cutoff distance enter 25
# if distance greater than 25km
# print("good news your charge is aplicable for only remaining of 25")
# chargeble distance= distance-cutoff
# then enter the charge
# print the total cost
#otherwise
# print free ride
try:
distance =eval(input('enter distance :'))
if distance <= 25:
print('"good news your charge is aplicable for only remaining of 25"')
else:
charge=eval(input('enter charge :'))
total = distance*charge
print(f'total charge {total} Rs. of distance {distance}')
except Exception as e:
print(e)
total charge 30456 Rs. of distance 564
In [9]: # wap ask the user enter the course
# ask the user enter the Institute
# if the course equal to data science and institute equal to naresh it
# then you are good
# otherwise
# you are bad
try:
course = str(input('Enter Course name : '))
institute = str(input('Enter Institute name : '))
if course== 'Data science' and institute == 'NareshIt':
print('you are good')
else :
print('you are bad')
except Exception as e:
print(e)
you are bad
In [10]: # wap ask the user enter a random number between 1 to 10, treat this as number1
# ask the user enter another number from keyboard, treat this as number2
# if number1 equal to number2
# print you won
# otherwise
# print you lost
try:
number = random.randint(1,10)
num1=eval(input('Enter a number1 :'))
if number == num1:
file:///C:/Users/Shubham/Downloads/Python Assignment_6_Try_Except.html 2/4
01/08/2024, 09:47 Python Assignment_6_Try_Except
print (f'Random number :{number}, You enter : {num1} and you Won')
else:
print('Loss')
except Exception as e:
print(e)
Loss
In [11]: # wap ask the user enter number
# if number equal to 1 then print one
# if number equal to 2 then print two
# if number equal to 3 then print three
# otherwise print enter a valid number
try:
num = eval(input('Enter a number :'))
if num == 1:
print('number is one')
elif num==2:
print('number is two')
elif num==3:
print('number is three')
else:
print(f'number is {num}')
except Exception as e:
print(e)
number is one
In [13]: # wap ask the user enter a number
# if that number greater than zero print postive
# if that number less than zero print negative
# otherwise print zero
try:
num = eval(input('Enter the number :'))
if num >= 0:
if num ==0:
print('Zero')
else:
print('Positive number')
else :
print('negative number')
except Exception as e:
print(e)
Positive number
In [14]: # WAP ask the user enter the percentage of marks 0 to 100
# if percentagw gretaer than 90 print A garde
# if percentage between 75 to 90 print B garde
# if percentage between 50 to 75 print C grade
# if percentage between 35 to 50 print D grade
file:///C:/Users/Shubham/Downloads/Python Assignment_6_Try_Except.html 3/4
01/08/2024, 09:47 Python Assignment_6_Try_Except
# if percentage less than 35 print Fail
try:
percentage = eval(input('Enter a Percentage :'))
if percentage >= 90:
print('You got A garde')
elif percentage >= 75 :
print('You got B garde')
elif percentage >= 50 :
print('You got C garde')
elif percentage >= 35 :
print('You got D garde')
else:
print('fail')
except Exception as e:
print(e)
You got A garde
In [15]: # WAP ask the user enter the age
# if the age greater tahn 100 print you are lucky
# if the age gretaer than 75 print old age
# if the age between 50 to 75 print ss
# if the age between 30 tp 50 print MA
# if the age between 15 to 30 print young age
# if the afe between less than 15 print kid
try:
age = eval(input('Enter your age :'))
if age>100:
print('You are lucky')
elif age>= 70 :
print('You old age')
elif age>= 50 :
print('You are ss')
elif age>= 30 :
print('You are MA')
elif age>= 15 :
print('You are young age')
else:
print('Teenage')
except Exception as e:
print(e)
Teenage
In [ ]:
file:///C:/Users/Shubham/Downloads/Python Assignment_6_Try_Except.html 4/4