【问题标题】:I have a problem with a function, it does not return anything and when I call it and when I try to call it tells me that I have not defined variable我的函数有问题,它不返回任何内容,当我调用它以及尝试调用它时告诉我我没有定义变量
【发布时间】:2021-06-28 02:56:41
【问题描述】:
try:
    print("This program will allow you to calculate the average of your califications \nonly accepts 4 grades")
    
    opc=int(input("type (1) to calculate a simple average or type (2) \nto calculate the weighted average: "))
    n1 =int(input("Write your first calification: "))
    n2 = int(input("Write your second calification: "))
    n3= int(input("Write your third calification: "))
    n4= int(input("Write your fourth calification: "))
    def promedio(opc,prom):
        if opc == 1:# simple average calification
            p= (n1+n2+n3+n4)/4
            print("your average calification is : ",p)
             
        elif opc == 2:# weight average calification
            p1=int(input("insert the weighted of your first calification: "))
            p2=int(input("insert the weighted of your second calification: "))
            p3=int(input("insert the weighted of your third calification: "))
            p4=int(input("insert the weighted of your fourth calification: "))
            p=p1*n1+p2*n2+p3*n3+p4*n4
            print("your weighted average calification is: ",p)
            
        else:
            print("your average calification is")
        return prom
    x = promedio(p)
    print(x)

    
    
except:
    print("write only a number 1 or 2")

【问题讨论】:

  • 我希望你知道 x = promedio(p) 你传递了一个参数,但函数需要 2 个
  • 你没有给这个变量 p 赋值。你在参数中传递它
  • p 在函数内部定义。这将导致错误

标签: python function undefined


【解决方案1】:
            opc=int(input("type (1) to calculate a simple average or type (2) \nto calculate the weighted average: "))
            n1 =int(input("Write your first calification: "))
            n2 = int(input("Write your second calification: "))
            n3= int(input("Write your third calification: "))
            n4= int(input("Write your fourth calification: "))
            def promedio(opc,arr):
                if opc == 1:# simple average calification
                     p= (arr[0]+arr[1]+arr[2]+arr[3])/4
                     print("your average calification is : ",p)
     
                elif opc == 2:# weight average calification
                     p1=int(input("insert the weighted of your first calification: "))
                     p2=int(input("insert the weighted of your second calification: "))
                     p3=int(input("insert the weighted of your third calification: "))
                     p4=int(input("insert the weighted of your fourth calification: "))
                     p=p1*arr[0]+p2*arr[1]+p3*arr[2]+p4*arr[3]
                     print("your weighted average calification is: ",p)
    
                else:
                     print("your average calification is")
                return p
           arr=[n1,n2,n3,n4]
           x = promedio(opc,arr)
           print(x)

【讨论】:

    【解决方案2】:

    根据@Sujay 的建议,promedio 中需要的函数参数应该修改,返回值应该是p

    try:
        print("This program will allow you to calculate the average of your califications \nonly accepts 4 grades")
        
        opc=int(input("type (1) to calculate a simple average or type (2) \nto calculate the weighted average: "))
        n1 =int(input("Write your first calification: "))
        n2 = int(input("Write your second calification: "))
        n3= int(input("Write your third calification: "))
        n4= int(input("Write your fourth calification: "))
        def promedio(opc):
            if opc == 1:# simple average calification
                p= (n1+n2+n3+n4)/4
                print("your average calification is : ",p)
                 
            elif opc == 2:# weight average calification
                p1=int(input("insert the weighted of your first calification: "))
                p2=int(input("insert the weighted of your second calification: "))
                p3=int(input("insert the weighted of your third calification: "))
                p4=int(input("insert the weighted of your fourth calification: "))
                p=p1*n1+p2*n2+p3*n3+p4*n4
                print("your weighted average calification is: ",p)
                
            else:
                print("your average calification is")
            return p
        x = promedio(opc)
        print(x)
    except:
        print("write only a number 1 or 2")
    

    【讨论】:

      猜你喜欢
      • 2020-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多