简体   繁体   English

赋值之前引用的局部变量“ a”

[英]local variable 'a' referenced before assignment

i have an assignment that is asking me to create a module with 2 functions. 我有一个作业要我创建具有2个功能的模块。 The user is to enter the 3 sides of a triangle, and the two functions will check (a) if the input is valid and (b) what the area of the triangle is. 用户将输入三角形的3个边,这两个功能将检查(a)输入是否有效,以及(b)三角形的面积是多少。

 def isValid(s1,s2,s3):

    if (s1 + s2<=s3):
         print("Input is invalid.")
    else:
         area(s1,s2,s3)


 def area(s1,s2,s3):
     p = (s1+s2+s3)/2
     a = (p*(p-a)*(p-b)*(p-c))**(.5)
     print("The area of the triangle is: ",a)


s1 = eval(input("input s1: "))
s2 = eval(input("input s2: "))
s3 = eval(input("input s3: "))

isValid(s1,s2,s3)

im getting an error of: UnboundLocalError: local variable 'a' referenced before assignment 即时通讯收到错误: UnboundLocalError: local variable 'a' referenced赋值之前UnboundLocalError: local variable 'a' referenced

not sure where to go from here, getting a little confused. 不知道从哪里去,变得有些困惑。 Any help would be appreciated. 任何帮助,将不胜感激。

You are using the variables a , b and c without having defined them, when calculating the area (stored in the variable a again): 在计算面积(再次存储在变量a )时,您使用的变量abc没有定义它们:

a = (p*(p-a)*(p-b)*(p-c))**(.5)

Perhaps you wanted to use s1 , s2 and s3 here instead: 也许您想在这里使用s1s2s3

a = (p * (p - s1) * (p - s2) * (p - s3)) ** .5

change function 改变功能

 def area(s1,s2,s3):
     p = (s1+s2+s3)/2
     a = (p*(p-a)*(p-b)*(p-c))**(.5)
     print("The area of the triangle is: ",a)

to

def area(s1,s2,s3):
    p = (s1+s2+s3)/2
    a = (p*(p-s1)*(p-s2)*(p-s3))**(.5)
    print("The area of the triangle is: ",a)

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

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