如何获取变量在函数内外工作

时间:2017-08-29 00:58:01

标签: python

我正在尝试制作简单的未加权gpa计算器,但我遇到了一个问题。我不能从我的函数内部创建值来进行最终计算。

#!/usr/bin/env python

print ("Welcome to the un-weighted gpa calculator!")

year_one_gpa = ' '

def year_one():
    number_classes = input("How many classes did you take your first year in highschool?(Please enter an integer, no percents): ")

    class_gpa = [ ]

    global year_one_gpa

    for i in range(1, number_classes + 1):
        grade = input("What was your final grade in the class: " )

        if grade >= 93 and grade <= 100:
            class_gpa.append(4.0)

        elif grade >= 90 and grade <= 92:
            class_gpa.append(3.7)

        elif grade >= 88 and grade <= 89:
            class_gpa.append(3.3)

        elif grade >= 83 and grade <= 87:
            class_gpa.append(3.0)

        elif grade >= 80 and grade <= 83:
            class_gpa.append(2.7)

        elif grade >= 78 and grade <= 79:
            class_gpa.append(2.3)

        elif grade >= 73 and grade <= 77:
            class_gpa.append(2.0)

        elif grade >= 70 and grade <= 72:
            class_gpa.append(1.7)

        elif grade >= 68 and grade <= 69:
            class_gpa.append(1.3)

        elif grade >= 65 and grade <= 67:
            class_gpa.append(1.0)

        else:
            class_gpa.append(0.0)



    return class_gpa

    year_one_gpa = (sum(class_gpa)/ float(len(class_gpa)))



year_one()

print year_one_gpa

这就是我到目前为止,每当打印year_one_gpa时,都会出现一个空白列表。

4 个答案:

答案 0 :(得分:1)

当您使用return时,您将走出该功能。

尝试替换它:

return class_gpa

year_one_gpa = (sum(class_gpa)/ float(len(class_gpa)))

由此:

year_one_gpa = (sum(class_gpa)/ float(len(class_gpa)))
return class_gpa

答案 1 :(得分:1)

首先,如果您正在编写一个包含超过4-5个案例的if / else语句,那么您的工作效率非常低。总是有一种比巨型elif弦更好的方式。

例如,您可以使用字典和地图值,或者您可以使用元组列表作为成绩的最小值。

像这样:

gradeToGpa = [ (93, 4.0) , (90, 3.7), (88, 3.3) ..... ]
然后,您可以将逻辑重述为:

for score, gpa in gradeToGpa:
    if score < classGrade:
        classGPA = gpa
        break

第二,你不应该使用范围(1,数字+ 1)你应该使用范围(0,数字),或者甚至更好,范围(数字)假设前面是0。

您使用的global术语非常错误,而在python中,您应该从函数返回一个元组。

此外,您的函数中有一个return语句,后跟代码。因为return语句总是在不相关之后执行任何代码。

return class_gpa   #leave the function here

year_one_gpa = (sum(class_gpa)/ float(len(class_gpa)))  # this will never run

相反,你应该这样写:

year_one_gpa = (sum(class_gpa)/ float(len(class_gpa)))

return year_one_gpa

并且,在函数之外你应该写:

my_gpa = year_one()

print my_gpa

最后,stackoverflow不是让人们调试代码的地方。很明显,在来这里让别人为你解决问题之前,你没有做过适当的阅读。我建议读一本关于python的书或阅读一些教程。返回语句对于所有编程都是绝对必要的。

答案 2 :(得分:0)

你犯了几个错误首先在函数外部声明了一个全局变量,只能在函数内部访问局部变量,所以修改后的代码将是:

func createAndconfigureActivityIndicatorView() {

        activityIndicatorView  = UIActivityIndicatorView(activityIndicatorStyle: .white) 

        activityIndicatorView.backgroundColor = UIColor.gray // Set Activity indicator backgroundColor 

        activityIndicatorView.startAnimating() // To start animation

        activityIndicatorView.hidesWhenStopped = true // Setting this true means, when you stop the activity indicator, its automatically hidden (see below)
}

// Stops and Hides Activity Indicator
activityIndicatorView.stopAnimating()

另外,您可能希望将year_one返回的值保存在我认为的变量中。

答案 3 :(得分:0)

只需在代码的第一行输入oModel.submitChanges(); oModel.setUseBatch(false); // Make false if you reuse this oModel. 即可!