函数不返回值

2024-05-16 17:57:52 发布

您现在位置:Python中文网/ 问答频道 /正文

请帮忙。我的getroofCalcs()函数没有返回变量。特别是此时的roofArea变量。这是我的编程入门类的一个基本程序,我不明白为什么在运行这个类时,我总是得到一个错误,即调用getshingleCalcs()函数时roofArea没有定义。这段代码是用Python编写的。你知道吗

# Stick Built Garage Estimator
# Written by: John Ruehs

#Initialization Variables

#Declare doAgain

#Input Variables

#Declare length
#Declare width
#Declare studSpace
#Declare wallHeight
#Declare roofPitch
#Declare overHang
#Declare bigGarageDoor
#Declare smallGarageDoor
#Declare entryDoor
#Declare window

#Calculated Variables

#Declare topTiePlate
#Declare bottomPlate
#Declare studs
#Declare wallSheathing
#Declare roofSheathing
#Declare shingles
#Declare shingleStarter
#Declare ridgeCap
#Declare roofArea
#Declare rakeLength
#Declare studAdj
#Declare botPlateAdj
#Declare wallAreaAdj
#Declare gableArea

import math

def main():
    doAgain = "yes"
    if doAgain == "yes":
        length, width, studSpace, wallHeight, roofPitch, overHang, bigGarageDoor, smallGarageDoor, entryDoor, windows = getinputs()
        getframeCalcs(length, width, studSpace, bigGarageDoor, smallGarageDoor, entryDoor, windows)
        getwallCalcs(length, width, wallHeight, bigGarageDoor, smallGarageDoor, entryDoor, roofPitch)#need to put variables needed here
        getroofCalcs(length, width, roofPitch, overHang)#need to put variables needed here
        getshingleCalcs(length, roofArea, rakeLength)#need to put variables needed here
        display(topTiePlate, bottomPlate, studs, wallSheathing, roofSheathing, shingles, shingleStarter, ridgeCap, rakeLength)#need to put variables needed here
        doAgain = input("Do you want to run this again('yes' or 'no')?")
    else:
        print("")


def getinputs():
    length = float(input("Enter the length of the building: "))
    width = float(input("Enter the width of the building: "))
    studSpace = float(input("Enter the stud spacing: "))
    wallHeight = float(input("Enter the wall height: "))
    roofPitch = input("Enter the roof pitch: ")
    overHang = float(input("Enter the over-hang in inches: "))
    bigGarageDoor = int(input("Enter the number of 16' garage doors: "))
    smallGarageDoor = int(input("Enter the number of 9' garage doors: "))
    entryDoor = int(input("Enter the number of entry doors: "))
    windows = int(input("Enter the number of windows that are smaller than 3' wide: "))
    return length, width, studSpace, wallHeight, roofPitch, overHang, bigGarageDoor, smallGarageDoor, entryDoor, windows

def getframeCalcs(length, width, studSpace, bigGarageDoor, smallGarageDoor, entryDoor, windows):
    studAdj = ((bigGarageDoor*-7)+(smallGarageDoor*-3)+(entryDoor*2)+(windows*5))
    botPlateAdj = ((bigGarageDoor*-16)+(smallGarageDoor*-9)+(entryDoor*-3))
    studs = math.ceil((((((((length*2)+(width*2))*12)/studSpace)+8)*1.1)+studAdj))
    topTiePlate = math.ceil((((length*2)+(width*2))/16)*2)
    bottomPlate = math.ceil(((((length*2)+(width*2))+botPlateAdj)/16))
    return studs, topTiePlate, bottomPlate

def getwallCalcs(length, width, wallHeight, bigGarageDoor, smallGarageDoor, entryDoor, roofPitch):
    wallAreaAdj = ((bigGarageDoor*-112)+(smallGarageDoor*-63)+(entryDoor*-21.77))
    if roofPitch == "1/12":
        gableArea = math.ceil(((((width/2)+0.5)*1)/12)*(((width/2)+0.5))*2)
    elif roofPitch == "2/12":
        gableArea = math.ceil(((((width/2)+0.5)*2)/12)*(((width/2)+0.5))*2)
    elif roofPitch == "3/12":
        gableArea = math.ceil(((((width/2)+0.5)*3)/12)*(((width/2)+0.5))*2)
    elif roofPitch == "4/12":
        gableArea = math.ceil(((((width/2)+0.5)*4)/12)*(((width/2)+0.5))*2)
    elif roofPitch == "5/12":
        gableArea = math.ceil(((((width/2)+0.5)*5)/12)*(((width/2)+0.5))*2)
    elif roofPitch == "6/12":
        gableArea = math.ceil(((((width/2)+0.5)*6)/12)*(((width/2)+0.5))*2)
    elif roofPitch == "7/12":
        gableArea = math.ceil(((((width/2)+0.5)*7)/12)*(((width/2)+0.5))*2)
    elif roofPitch == "8/12":
        gableArea = math.ceil(((((width/2)+0.5)*8)/12)*(((width/2)+0.5))*2)
    elif roofPitch == "9/12":
        gableArea = math.ceil(((((width/2)+0.5)*9)/12)*(((width/2)+0.5))*2)
    elif roofPitch == "10/12":
        gableArea = math.ceil(((((width/2)+0.5)*10)/12)*(((width/2)+0.5))*2)
    elif roofPitch == "11/12":
        gableArea = math.ceil(((((width/2)+0.5)*11)/12)*(((width/2)+0.5))*2)
    else:
        gabelArea = math.ceil(((((width/2)+0.5)*12)/12)*(((width/2)+0.5))*2)
    wallSheathing = math.ceil(((((((length*2)+(width*2))*wallHeight)+gableArea)+wallAreaAdj)/32))
    return wallSheathing

def getroofCalcs(length, width, roofPitch, overHang):
    if roofPitch == "1/12":
        roofArea = math.ceil((((((((((width/2)+(overHang/12))*1)/12)**2)+(((width/2)+(overHang/12))**2))**.5)*length)*2))
    elif roofPitch == "2/12":
        roofArea = math.ceil((((((((((width/2)+(overHang/12))*2)/12)**2)+(((width/2)+(overHang/12))**2))**.5)*length)*2))
    elif roofPitch == "3/12":
        roofArea = math.ceil((((((((((width/2)+(overHang/12))*3)/12)**2)+(((width/2)+(overHang/12))**2))**.5)*length)*2))
    elif roofPitch == "4/12":
        roofArea = math.ceil((((((((((width/2)+(overHang/12))*4)/12)**2)+(((width/2)+(overHang/12))**2))**.5)*length)*2))
    elif roofPitch == "5/12":
        roofArea = math.ceil((((((((((width/2)+(overHang/12))*5)/12)**2)+(((width/2)+(overHang/12))**2))**.5)*length)*2))
    elif roofPitch == "6/12":
        roofArea = math.ceil((((((((((width/2)+(overHang/12))*6)/12)**2)+(((width/2)+(overHang/12))**2))**.5)*length)*2))
    elif roofPitch == "7/12":
        roofArea = math.ceil((((((((((width/2)+(overHang/12))*7)/12)**2)+(((width/2)+(overHang/12))**2))**.5)*length)*2))
    elif roofPitch == "8/12":
        roofArea = math.ceil((((((((((width/2)+(overHang/12))*8)/12)**2)+(((width/2)+(overHang/12))**2))**.5)*length)*2))
    elif roofPitch == "9/12":
        roofArea = math.ceil((((((((((width/2)+(overHang/12))*9)/12)**2)+(((width/2)+(overHang/12))**2))**.5)*length)*2))
    elif roofPitch == "10/12":
        roofArea = math.ceil((((((((((width/2)+(overHang/12))*10)/12)**2)+(((width/2)+(overHang/12))**2))**.5)*length)*2))
    elif roofPitch == "11/12":
        roofArea = math.ceil((((((((((width/2)+(overHang/12))*11)/12)**2)+(((width/2)+(overHang/12))**2))**.5)*length)*2))
    else:
        roofArea = math.ceil((((((((((width/2)+(overHang/12))*12)/12)**2)+(((width/2)+(overHang/12))**2))**.5)*length)*2))

    roofSheathing = math.ceil(roofArea/32)

    if roofPitch == "1/12":
        rakeLength = math.ceil((((((((width/2)+(overHang/12))*1)/12)**2)+(((width/2)+(overHang/12))**2))**.5))
    elif roofPitch == "2/12":
        rakeLength = math.ceil((((((((width/2)+(overHang/12))*2)/12)**2)+(((width/2)+(overHang/12))**2))**.5))
    elif roofPitch == "3/12":
        rakeLength = math.ceil((((((((width/2)+(overHang/12))*3)/12)**2)+(((width/2)+(overHang/12))**2))**.5))
    elif roofPitch == "4/12":
        rakeLength = math.ceil((((((((width/2)+(overHang/12))*4)/12)**2)+(((width/2)+(overHang/12))**2))**.5))
    elif roofPitch == "5/12":
        rakeLength = math.ceil((((((((width/2)+(overHang/12))*5)/12)**2)+(((width/2)+(overHang/12))**2))**.5))
    elif roofPitch == "6/12":
        rakeLength = math.ceil((((((((width/2)+(overHang/12))*6)/12)**2)+(((width/2)+(overHang/12))**2))**.5))
    elif roofPitch == "7/12":
        rakeLength = math.ceil((((((((width/2)+(overHang/12))*7)/12)**2)+(((width/2)+(overHang/12))**2))**.5))
    elif roofPitch == "8/12":
        rakeLength = math.ceil((((((((width/2)+(overHang/12))*8)/12)**2)+(((width/2)+(overHang/12))**2))**.5))
    elif roofPitch == "9/12":
        rakeLength = math.ceil((((((((width/2)+(overHang/12))*9)/12)**2)+(((width/2)+(overHang/12))**2))**.5))
    elif roofPitch == "10/12":
        rakeLength = math.ceil((((((((width/2)+(overHang/12))*10)/12)**2)+(((width/2)+(overHang/12))**2))**.5))
    elif roofPitch == "11/12":
        rakeLength = math.ceil((((((((width/2)+(overHang/12))*11)/12)**2)+(((width/2)+(overHang/12))**2))**.5))
    else:
        rakeLength = math.ceil((((((((width/2)+(overHang/12))*12)/12)**2)+(((width/2)+(overHang/12))**2))**.5))
    return roofArea, roofSheathing, rakeLength

def getshingleCalcs(length, roofArea, rakeLength):
    shingles = math.ceil(((roofArea/100)*3))
    shingleStarter = math.ceil((((rakeLength*4)+(length*2))/120))
    ridgeCap = math.ceil(length/20)
    return shingles, shingleStarter, ridgeCap

def display(topTiePlate, bottomPlate, studs, wallSheathing, roofSheathing, shingles, shingleStarter, ridgeCap, rakeLength):
    print("")
    print("16' Top Plate/Tie Plate: ", topTiePlate)
    print("16' Bottom Plate: ", bottomPlate)
    print("Studs: ", studs)
    print("4'x8' Wall Sheathing: ", wallSheathing)
    print("4'x8' Roof Sheathing: ", roofSheathing)
    print("Rake Length (Rounded Up): ", rakeLength)
    print("Bundles of Shingles: ", shingles)
    print("Bundles of Shingle Starter: ", shingleStarter)
    print("Bundles of Ridge Cap: ", ridgeCap)
    print("")
    print("")

main()

Tags: themathwidthlengthprintelifdeclareceil
1条回答
网友
1楼 · 发布于 2024-05-16 17:57:52

getroofCalcs()返回一个值-返回一个由三个计算值组成的元组。但是,问题是返回值没有绑定到任何变量,因此丢失了。您可以更改在main()中调用getroofCalcs()的代码,以将函数的返回值绑定到变量:

    result = getroofCalcs(length, width, roofPitch, overHang)#need to put variables needed here

这将绑定到变量resultgetroofCalcs()返回的元组。也可以将元组直接解包到单个变量中,如下所示:

    roofArea, roofSheathing, rakeLength = getroofCalcs(length, width, roofPitch, overHang)

现在对getshingleCalcs()的调用应该可以工作了。你知道吗

N.B.getshingleCalcs()的调用也有类似的问题,返回值丢失,因为它没有绑定到任何变量。您还应将该行更改为:

    shingles, shingleStarter, ridgeCap = getshingleCalcs(length, roofArea, rakeLength)

相关问题 更多 >