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

2024-04-26 02:20:42 发布

您现在位置: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)

注意:我之所以在函数areaOfTriangle(Z)中传递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

在这一行Enter height and base :我按了回车键,导致了这个错误。


Tags: andoftheinputareachfloatlength
1条回答
网友
1楼 · 发布于 2024-04-26 02:20:42

最后一个错误是解决你的问题:

UnboundLocalError: local variable 'ch' referenced before assignment

这意味着变量ch没有分配给任何值,并且正在被访问,这就产生了问题。

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

您认为在第一次调用中分配的ch值在其他递归中也将保持不变,但这并不是您所想的那样。

相关问题 更多 >