简体   繁体   中英

During handling of the above exception, another exception occurred: Python program

This is a simple python program for calculating area of triangle which is not working for all test cases. I've doubt that the first try: block of the program is creating the problem but I'm not sure about this. May be the value for ch is not getting specified so this may create more problems.

The complete code is :-

# area of traingle
import cmath
import sys

def areaOfTriangle(z):
    flag = 0
    a, b, c, angle, area = 0, 0, 0, 0, 0
    print(" What information about the tiangle you have? ")
    print(" 1. Height and Base : ")
    print(" 2. Length of three sides of triange : ")
    print(" 3. Length of two sides and angle between them : ")
    print(" 4. EXIT : \n")
    try:
        if flag == 0 and z == 2:
            ch = int(input("Enter your choice : "))
    except:
        print("OOPS..!! something went wrong, try again")
        areaOfTriangle(2)
    if ch == 1:
        try:
            a = float(input(" Enter height and base : "))
            b = float(input())
        except:
            print("OOPS..!! something went wrong, try again")
            flag = 1
            areaOfTriangle(1)
        area = (a*b)/2
    elif ch == 2:
        try:
            a = float(input(" Enter length of three sides : "))
            b = float(input())
            c = float(input())
        except:
            print("OOPS..!! Something went wrong, try again")
            flag = 1
            areaOfTriangle(1)
        s = (a+b+c)/2
        area = cmath.sqrt((s) * (s-a) * (s-b) * (s-c))
        area = abs(area)
    elif ch == 3:
        try:
            a = float(input("Enter the base length : "))
            b = float(input("Enter the side length : "))
            angle = float(input("Enter the angle between the two sides : "))
        except:
            print("OOPS..!! Something went wrong, try again")
            flag = 1
            areaOfTriangle(1)
        area = (1/2) * a * b * cmath.sin(angle)
        area = abs(area)
    elif ch == 4:
        sys.exit(0)
    else:
        print("wrong choice")
    print("The area of the triangle is ", area)
    return
areaOfTriangle(2)

NOTE : I'm passing z in the function areaOfTriangle(Z) only because I don't want the user to enter the choice again and again if any exception occurs after entering the choice once.

The Error which testing for different case is : -

amitwebhero@AmitKali:~$ python3.5 ~/python/basic\ programs/2-area-triange.py 
 What information about the tiangle you have? 
 1. Height and Base : 
 2. Length of three sides of triange : 
 3. Length of two sides and angle between them : 
 4. EXIT : 

Enter your choice : 1
 Enter height and base : 
OOPS..!! something went wrong, try again
 What information about the tiangle you have? 
 1. Height and Base : 
 2. Length of three sides of triange : 
 3. Length of two sides and angle between them : 
 4. EXIT : 

Traceback (most recent call last):
  File "/home/amitwebhero/python/basic programs/2-area-triange.py", line 22, in areaOfTriangle
    a = float(input(" Enter height and base : "))
ValueError: could not convert string to float: 

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/amitwebhero/python/basic programs/2-area-triange.py", line 58, in <module>
    areaOfTriangle(2)
  File "/home/amitwebhero/python/basic programs/2-area-triange.py", line 27, in areaOfTriangle
    areaOfTriangle(1)
  File "/home/amitwebhero/python/basic programs/2-area-triange.py", line 20, in areaOfTriangle
    if ch == 1:
UnboundLocalError: local variable 'ch' referenced before assignment

Here on this line Enter height and base : I pressed Enter key which created this error.

The final error is the solution to your problem :-

UnboundLocalError: local variable 'ch' referenced before assignment

It means variable ch is not assigned to any value and it is being accessed which creates the problem.

In line 27 of the code , your are calling areaOfTriangle(1) and this recursion assigns the value of z = 1 and this does not let your ch to be assigned because your if condition returns False.

You are thinking that the value of ch assigned in first call will remain same in the other recursion also but this does not happen exactly the way you thought.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM