在变量

时间:2016-09-06 22:39:02

标签: python function csv

我只是在Python中使用函数(仅学习Python 6个月),而且我仍然坚持使用一些无效的代码。它说总数没有定义,名称错误。阅读一些帖子我认为我需要将总数存储在一个变量中,但我不知道在哪里。你能在退货声明中这样做吗?不知道在哪里定义总数以使其全局化。

这是一项包含多项任务的计划。我还在努力将表存储到csv文件中。这是代码。

import csv

def set_values():
    ans1 = float(input('Please enter the first number: '))

    ans2 = float(input('Please enter the second number: '))

    ans3 = float(input('Please enter the third number: '))

    levels = int(input('Please set the amount of levels between 5 and 10: '))


    return (ans1, ans2, ans3, levels) 


def display_model_values(ans1, ans2, ans3, levels):
    print('The outcome for model 1 is ',ans1)
    print('The outcome for model 2 is ',ans2)
    print('The outcome for model 3 is ',ans3)
    print('The number of levels are ',levels)


def run_model(ans1, ans2, ans3, levels):
    total = ans1+ans2+ans3
    print ("\t","Level","\t","Answer 1","\t","Answer 2","\t","Answer 3","\t","Total")
    for i in range (0,levels+1):
        print("\t",i,"\t\t",ans1,"\t\t",ans2,"\t\t",ans3,"\t\t",total)
        result1 =ans2*ans3
        result2 = ans2/ans1
        total = ans1+result1+result2
    return (i,result1, result2, total)


def export_data(ans1,ans2,ans3,total):
    table = [ans1, ans2, ans3,total]

    nameoffile = input('what would you like to call the filename')
    nameoffile = open(nameoffile+".csv","w")
    csv_file_ref = csv.writer(nameoffile)
    csv_file_ref.writerow(table)
    nameoffile.close()

##    with open(nameoffile+'.csv', 'w') as csvfile:
##        writer = csv.writer(csvfile)
##        writer.writerow(r) for r in table]




choice = ''
count = 0
while choice != 'q':
    print('Main Menu')
    print ('1)Set Model Values')
    print ('2)Display Model Values')
    print ('3)Run Model')
    print ('4)Export Data')
    print ('Q)Quit')
    choice = input('Please Enter Choice')


    if choice =='1':
        ans1, ans2, ans3, levels = set_values()
        count = count +1

    elif choice == '2':
        if count < 1:
           print ('you need to choose option 1 first')
        else:
            display_model_values(ans1,ans2,ans3,levels)


    elif choice =='3':
        if count < 1:
            print('you need to choose option 1 first')
        else:
            run_model(ans1,ans2,ans3,levels)

    elif choice =='4':
        if count < 1:
           print ('you need to choose option 1 first')
        else:
            export_data(ans1,ans2,ans3,total)

    elif choice == 'Q':
            break
    else:
        print('not an option')

3 个答案:

答案 0 :(得分:1)

在第83行,您将变量total发送到函数export_data(ans1,ans2,ans3,total),该函数未在while循环中定义。假设在发送值之前您的总数为ans1+ans2+ans3, 添加行,

total = ans1+ans2+ans3

那应该可以解决问题。

答案 1 :(得分:1)

您的总数仅在函数run_model内定义。当该函数返回时,您无法再次引用total,因为它已经消失out of scope。该变量现在是#34; unbound&#34;,这正是Python在说明名称total未定义时告诉你的。它曾被定义过一次,但它已经很久了。

对代码进行简单的更改就是在导出函数体内再次计算总数,如下所示:

def export_data(ans1,ans2,ans3):
    total = ans1 + ans2 + ans3  # total is available inside of export_data
    table = [ans1, ans2, ans3,total]

    nameoffile = input('what would you like to call the filename')
    nameoffile = open(nameoffile+".csv","w")
    csv_file_ref = csv.writer(nameoffile)
    csv_file_ref.writerow(table)
    nameoffile.close()

这些应该使您的代码有效。

答案 2 :(得分:0)

def export_data(ans1,ans2,ans3):     total = ans1 + ans2 + ans3#total在export_data中可用     table = [ans1,ans2,ans3,total]

nameoffile = input('what would you like to call the filename')
nameoffile = open(nameoffile+".csv","w")
csv_file_ref = csv.writer(nameoffile)
csv_file_ref.writerow(table)
nameoffile.close()

现在它导出并且总共没有错误