Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Exception handling
#1
A simple python calculator i wrote
#!/usr/bin/python

print("Please select your desire operation")
print("1. Addition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")

#this function is to add two numbers
def add(a,b):
    x = a + b
    return x
#this function is to subtract the two numbers
def subs(a,b):
    x = a - b
    return x
#this function is for multiplication
def muls(a,b):
    x = a * b
    return x
#this function is for division
def divs(a,b):
    x = a / b
    return x

try:
    insert = int(input("Enter your choice: "))
except (ValueError,KeyboardInterrupt):
    print ("You must type the number between 1 to 4")
else:
    if insert == 1:
        a = int(input("Enter the first number to add: "))
        b = int(input("Enter the second number to add: "))
        print (add(a,b))
    elif insert == 2:
        a = int(input("Enter the first number to subtract: "))
        b = int(input("Enter the second number to subtract: "))
        print (subs(a,b))
    elif insert == 3:
        a = int(input("Enter the first number to multiply: "))
        b = int(input("Enter the second number to multiply: "))
        print (muls(a,b))
    elif insert == 4:
        a = int(input("Enter the first number to divide: "))
        b = int(input("Enter the second number to divide: "))
        print (muls(a,b))
    else:
        print ("Some unexpected error occurred")
The first exception works like charm but when i am inside the if loop the program output's the error
Please select your desire operation
1. Addition
2. Subtraction
3. Multiplication
4. Division
Enter your choice: 1
Enter the first number to add: 2
Enter the second number to add: abcd
Traceback (most recent call last):
  File "Calculator3.py", line 33, in <module>
    b = int(input("Enter the second number to add: "))
ValueError: invalid literal for int() with base 10: 'abcd'
How do i write a program which will accept exception error inside the if loop?
Reply
#2
try:
    # code
except ValueError:
    # exception code
The tricky part is what do you put in the #code and #exception code parts.

You could use an exception handler to make sure all inputs are integers. This one keeps asking until you enter an integer.
def get_input(prompt):
    while True:
        try:
            return int(input(prompt))
        except ValueError:
            print('Inputs must be integer')
Or you could put the try/except around your entire code and just print "Oopsy! Some error occurred"

You have another exception to check for (ZeroDivisionError).
Reply
#3
i will try to put the try/except around my entire code thank you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Star python exception handling handling .... with traceback mg24 3 1,289 Nov-09-2022, 07:29 PM
Last Post: Gribouillis
  TicTacToe Game Add Exception Handling and Warning Function ShaikhShaikh 5 2,432 Nov-03-2021, 05:02 PM
Last Post: deanhystad
  Exception handling in regex using python ShruthiLS 1 2,373 May-04-2020, 08:12 AM
Last Post: anbu23
  Handling exception from a module dchi2 11 5,634 Nov-25-2019, 08:47 AM
Last Post: dchi2
  problem using custom exception handling in python srm 3 3,070 Jul-03-2019, 09:10 PM
Last Post: ichabod801
  an easy way to disable exception handling Skaperen 6 5,481 Jun-02-2019, 10:38 PM
Last Post: Gribouillis
  exception handling KyawMyo 3 2,868 May-07-2019, 07:53 AM
Last Post: buran
  Database operation exception handling LostInCode 1 2,496 Jan-03-2019, 07:50 PM
Last Post: jeanMichelBain
  During handling of the above exception, another exception occurred Skaperen 7 26,927 Dec-21-2018, 10:58 AM
Last Post: Gribouillis
  Exception Handling grkiran2011 3 2,989 Oct-05-2018, 03:23 PM
Last Post: volcano63

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020