函数未返回值

时间:2016-08-06 04:11:53

标签: python function return

请帮忙。我的getroofCalcs()函数没有返回变量的问题。特别是目前的roofArea变量。这是我编程类介绍的一个基本程序,我无法弄清楚为什么当我运行这个时,我不断收到roofArea在调用getshingleCalcs()函数时没有定义的错误。这段代码是用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()

1 个答案:

答案 0 :(得分:1)

<link rel="stylesheet" href="http://harvesthq.github.io/chosen/chosen.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script> <script src="http://harvesthq.github.io/chosen/chosen.jquery.js" type="text/javascript"></script> <div> <em>Multiple Select with Groups</em><br> <select data-placeholder="Your Favorite Football Team" style="width:350px;" class="chosen-select" multiple tabindex="6"> <option value=""></option> <optgroup label="NFC EAST"> <option>Dallas Cowboys</option> <option>New York Giants</option> <option>Philadelphia Eagles</option> <option>Washington Redskins</option> </optgroup> </select> <select data-placeholder="Your Favorite Football Team" style="width:350px;" class="chosen-select" multiple tabindex="6"> <option value=""></option> <optgroup label="NFC EAST"> <option>Dallas Cowboys</option> <option>New York Giants</option> <option>Philadelphia Eagles</option> <option>Washington Redskins</option> </optgroup> </select> 返回一个值 - 它返回一个由三个计算值组成的元组。但问题是返回值没有绑定到任何变量,因此丢失了。您可以更改在getroofCalcs()中调用getroofCalcs()的代码,以将函数的返回值绑定到变量:

main()

这将绑定 result = getroofCalcs(length, width, roofPitch, overHang)#need to put variables needed here 返回的元组的变量result。也可以将元组直接解压缩到各个变量中,如下所示:

getroofCalcs()

现在对 roofArea, roofSheathing, rakeLength = getroofCalcs(length, width, roofPitch, overHang) 的调用应该有效。

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

getshingleCalcs()