简体   繁体   English

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

[英]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. 这是一个用于计算三角形面积的简单python程序,不适用于所有测试用例。 I've doubt that the first try: block of the program is creating the problem but I'm not sure about this. 我怀疑程序的第一个try:块会造成问题,但是我不确定。 May be the value for ch is not getting specified so this may create more problems. 可能是未指定ch的值,因此这可能会引起更多问题。

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. 注意:我在函数areaOfTriangle(Z)传递z仅仅是因为我不希望用户一次又一次输入选择,如果在输入一次选择后发生任何异常。

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. 在此行中Enter height and base :我按Enter键,从而导致此错误。

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. 这意味着变量ch未分配任何值,并且正在访问它,这会造成问题。

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. 27 of the code27 of the code27 of the code ,您正在调用areaOfTriangle(1)并且此递归指定z = 1的值,并且由于您的if条件返回False,所以这不让您的ch被分配。

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. 您以为在第一次调用中分配的ch的值在其他递归中也将保持不变,但这并没有完全按照您的想法发生。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在处理上述异常的过程中,又发生了一个异常 - During handling of the above exception, another exception occurred KeyError: 1 在处理上述异常的过程中,又发生了一个异常: - KeyError: 1 During handling of the above exception, another exception occurred: 在处理上述异常的过程中,又发生了一个异常:&amp; google sheet not found - During handling of the above exception, another exception occurred: & google sheets not found “在处理上述异常过程中,发生了另一个异常”for循环中的第一个输入 - “ during the handling of above exception, another exception occurred ” for the first input in the for loop 如何解决“在处理上述异常期间,发生了另一个异常:” - How to fix "During handling of the above exception, another exception occurred: " Flask-restful - 在处理上述异常的过程中,发生了另一个异常 - Flask-restful - During handling of the above exception, another exception occurred 关键错误:1 在处理上述异常的过程中,发生了另一个异常 - Key Error: 1 During handling of the above exception, another exception occurred KeyError:在处理上述异常的过程中,发生了另一个异常 - KeyError: During handling of the above exception, another exception occurred 在处理上述异常的过程中,发生了另一个异常:'Date' - During handling of the above exception, another exception occurred: 'Date' 在处理上述异常期间,发生了另一个异常 - During handling of the above exception, another exception occured
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM