Python Exercise12

Write a program to take input of integers from the user and divide the numbers

handle TypeError and ZeroDivisionError in your program

try:

a=input(“Please enter the dividend : “)

b=input(“Please enter the divisor : “)

print (a/b)

except TypeError:

print( “Both values must be integer”)

except ZeroDivisionError:

print (“Divisor must not be zero”)

Write a program to enter the amount to withdraw from ATM handle your errors

counter=1

while True:

amount=input(“Enter the amount you wish to withdraw : “)

try:

print(int(amount))

print(“Dispensing your cash”)

break

except ValueError:

print(“Pl. check the amount you entered”)

if counter < 4:

counter+=1

else:

print(“I am fed up with you”)

break

Write a Program to take input from the users and handle errors in your program

 

b=True

while b:

try:

x=input(“Please enter a number:”)

y=input(“Please enter a number:”)

x=int(x)

y=int(y)

z=x/y

except ZeroDivisionError as err:

print( “Handling run-time error”,err)

b=False

except ValueError as ver:

print (“Both values must be integer”,ver)

b=False

else:

print (“Division of two numbers: “,z)

Write a program to handle errors and also use finally

try:

x = float(input(“Your number: “))

inverse = 1.0 / x

print(inverse)

except ValueError:

print( “You should have given either an int or a float”)

except ZeroDivisionError:

print (“Number cannot be divided by zero”)

finally:

print (“There may or may not have been an exception.”)

Write a program to raise your own exception

def func(level):

if level < 1:

raise Exception(“Invalid level, should be greater than 1”)

else:

print(“Level greater than 1”)

func(1)

func(0)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s