在处理上述异常期间,发生了另一个异常:Python程序

时间:2015-11-06 19:46:23

标签: python

这是一个简单的python程序,用于计算三角形区域,它不适用于所有测试用例。我怀疑程序的第一个try:块是否会产生问题,但我对此并不确定。可能是ch的值未被指定,因此这可能会产生更多问题。

完整的代码是: -

# 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)

注意:我在z函数中传递areaOfTriangle(Z)只是因为我不希望用户在输入选择后再次出现任何异常时再次输入选择一次。

测试不同情况的错误是: -

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

在这一行[{1}}上我按了Enter键创建了这个错误。

1 个答案:

答案 0 :(得分:2)

最终错误是您问题的解决方案: -

UnboundLocalError: local variable 'ch' referenced before assignment

这意味着变量ch未分配给任何值,并且正在访问它而产生问题。

在行27 of the code中,您正在调用areaOfTriangle(1)并且此递归会分配z = 1的值,这不会让您的ch被分配,因为您的if条件返回False。

您认为第一次调用中分配的ch的值在另一次递归中也将保持相同,但这并不完全按照您的想法发生。