2015-11-06 33 views
0

這是一個簡單的python程序,用於計算不適用於所有測試用例的三角形面積。我懷疑該程序的第一個try:區塊正在產生問題,但我不確定這一點。可能是ch的值沒有被指定,所以這可能會產生更多的問題。在處理上述異常的過程中,發生了另一個異常:Python程序

完整的代碼是: -

# 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只是因爲我不希望用戶如果進入後出現任何異常,一次又一次進入選擇選擇一次。

該測試針對不同的情況下,錯誤是: -

[email protected]:~$ 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 :我按下回車這創造這個錯誤的關鍵。

+1

您對ch的賦值是在一個if塊中,這意味着如果'flag == 0和z == 2:'返回false,'ch'永遠不會被實際賦值 –

+0

這是第一次分配給我通過傳遞2作爲參數來調用函數,其餘時間我不想改變ch的值,所以我保留z爲1 –

+1

你遞歸地調用你的函數。堆棧中的每個調用都有自己的一組局部變量。沒有「第一次」,因爲你沒有一個循環可以多次執行任何操作。 – dsh

回答

2

最後的錯誤是解決問題的方法: -

UnboundLocalError: local variable 'ch' referenced before assignment

這意味着可變通道沒有分配到任何價值,它正在訪問其產生的問題。

在行27 of the code,你的電話號碼是areaOfTriangle(1),這個遞歸賦值爲z = 1,這不會讓你的ch被賦值,因爲你的if條件返回False。

您正在考慮在第一次調用中分配的ch的值在其他遞歸中也會保持不變,但這並不完全如您所想。