Python 3 - 为什么此变量未在此函数中定义

时间:2016-11-18 14:20:45

标签: python-3.x

因此,当我运行脚本时,它一直说“销售没有定义”#39;我只是缺少一些明显的东西吗?我是python的新手。为了更具体一点,它说明了我们的问题'列出(销售,所有者)'。我感谢任何帮助我的人,我希望你过得愉快。

def launch():
    sales = [0] * 7
    sales[0] = float(125900)
    sales[1] = float(115000)
    sales[2] = float(105900)
    sales[3] = float(85000)
    sales[4] = float(150000)
    sales[5] = float(155249)
    sales[6] = float(97500)
    owner = [0] * 7
    owner[0] = "Carson"
    owner[1] = "Smith"
    owner[2] = "Jackson"
    owner[3] = "Swanson"
    owner[4] = "Perry"
    owner[5] = "Beufort"
    owner[6] = "Anderson"
    return sales, owner
def listing(sales, owner):
    count = 0
    count2 = 1
    while count < 7:
        print(count2 , "" , ":" , "" , "owner[count]\t" , "$" , "" , format(sales[count],',.2f'))
        count = count + 1
        count2 = count2 + 1
def main():
    print("Welcome to the Botany Bay home sales calculator")
    print("This program will calculate the average selling price of the homes")
    print("sold this past year.  It will then determine how many homes sold")
    print("above the average and how many homes sold below the average.")
        print("=======================================================================")
    print("")
    print("Botany Bay Home Sales")
    print("*********************************")
    listing(sales, owner)

launch()
main()

2 个答案:

答案 0 :(得分:0)

函数中定义的名称是该函数的本地名称,仅在其中可见。主叫:

listing(sales, owner)
main中的

将失败,因为这些名称在launch之外不可见。

您需要从sales获取ownerlaunch的返回值,并将其传递给main。因此,最好将main定义为接受两个参数。

def main(sales, owner):
    # rest as is

然后,将值传递给main

sales, owner = launch()
main(sales, owner)

或者,因为您可能不希望salesowner绑定在此处:

main(launch())

如果将名称salesowner与:

绑定,请不要这样做
sales, owner = launch()

您不需要使用两个参数定义main,因为名称是全局绑定的(即listing(sales, owner)将找到它们)。我真的不建议这样做;将它们作为args传递给它们会比将它们更好(并且更快一些)。

答案 1 :(得分:0)

sales位于mains函数的本地范围内,而未在其中声明。 你可以做的是修改你的main函数来获得这样的2个参数:

def main(sales, owner):
    #do your stuff

然后使用launch()返回的两个对象,如下所示

main(*launch())

明星习惯于&#34;解包&#34;实际返回的两个对象只有一个launch(),即launch()返回(<type 'list'>, <type 'list'>),即包含两个对象的元组。 *launch()返回此元组的解压缩版本。

总结一下,你的整个代码将被(测试)

def launch():
    sales = [0] * 7
    sales[0] = float(125900)
    sales[1] = float(115000)
    sales[2] = float(105900)
    sales[3] = float(85000)
    sales[4] = float(150000)
    sales[5] = float(155249)
    sales[6] = float(97500)
    owner = [0] * 7
    owner[0] = "Carson"
    owner[1] = "Smith"
    owner[2] = "Jackson"
    owner[3] = "Swanson"
    owner[4] = "Perry"
    owner[5] = "Beufort"
    owner[6] = "Anderson"
    return sales, owner
def listing(sales, owner):
    count = 0
    count2 = 1
    while count < 7:
        print(count2 , "" , ":" , "" , "owner[count]\t" , "$" , "" , format(sales[count],',.2f'))
        count = count + 1
        count2 = count2 + 1
def main(sales, owner):
    print("Welcome to the Botany Bay home sales calculator")
    print("This program will calculate the average selling price of the homes")
    print("sold this past year.  It will then determine how many homes sold")
    print("above the average and how many homes sold below the average.")
    print("=======================================================================")
    print("")
    print("Botany Bay Home Sales")
    print("*********************************")
    listing(sales, owner)


main(*launch())