Python 传递Var数据b/w函数
好吧,我搜索了数据库,读了几篇文章,但没有一篇能回答我的问题,至少在我理解的方面。先到前提,我是Python的新手,但在C++和一些C++方面有一定的经验。在这些语言中,变量是“全局的”,并且只能有一个实例。现在使用python时,变量似乎是局部变量,并且可以有多个同名变量的实例。对于一个我必须为学校编写的程序,我被告知使用global传递数据是一种糟糕的做法(我不太理解这种想法)。所以我试图将数据从第一个函数传递到第二个函数。编写的代码使用了一个全局变量,但是我试图看看如何通过它们传递数据Python 传递Var数据b/w函数,python,Python,好吧,我搜索了数据库,读了几篇文章,但没有一篇能回答我的问题,至少在我理解的方面。先到前提,我是Python的新手,但在C++和一些C++方面有一定的经验。在这些语言中,变量是“全局的”,并且只能有一个实例。现在使用python时,变量似乎是局部变量,并且可以有多个同名变量的实例。对于一个我必须为学校编写的程序,我被告知使用global传递数据是一种糟糕的做法(我不太理解这种想法)。所以我试图将数据从第一个函数传递到第二个函数。编写的代码使用了一个全局变量,但是我试图看看如何通过它们传递数据
#modules
import time
#variables
testScores = []
#avg = 0.0
#functions
def calculate_average():
x = 0
print("Enter test scores")
while (x <=4): #limits number of inputs to 5 test scores as required per instructions for lab
test = input('Enter test score: ')
#if test == "":
#break
x += 1
testScores.append(float(test)) #adds input from test var into testScores list as a float
time.sleep(1)
print("","calculating average", ".........", "", sep = "\n")
time.sleep(1)
global avg #makes the var avg global so as to keep data stored so other functions can access the data
avg = float(sum(testScores)/len(testScores))#takes all the data points stored in the list testScores and adds them up via sum ,and then divdes by (len)gth (aka number of data points)
print("%.2f" % avg)#displays avg limiting to two decimal points rounding to the nearest 100th
def determine_grade(): #fucntion calls the var avg (which was made global in function 1) and converts it into a letter grade.
if (avg >= 90.0 and avg <= 100.0):
print("Grade = A")
elif (avg >= 80.0 and avg <= 89.9):
print("Grade = B")
elif (avg >= 70.0 and avg <= 79.9):
print("Grade = C")
elif (avg >= 60.0 and avg <= 69.9):
print("Grade = D")
elif (avg <= 59.9):
print("Grade = F")
def main():
print("Beginning Program","", sep = "\n")
time.sleep(1)
calculate_average()
determine_grade()
time.sleep(1)
print("","End of program...", "Have a good day...", "Program Terminated", sep = "\n")
main()
#模块
导入时间
#变数
测试分数=[]
#平均值=0.0
#功能
def计算_平均值():
x=0
打印(“输入测试分数”)
而(x=90.0,平均值=80.0,平均值=70.0,平均值=60.0,平均值见:
下面是一个简单的示例,说明如何使用分配给变量的返回值执行函数:
while True:
avg = calculate_average(testScores)
grade = determine_grade(avg)
print(grade)
time.sleep(1)
更新:
相关的:
您要做的是让calculateAverage()返回平均值,然后将平均值传递到Determine_grade()中
在愤怒中而不是
avg = float(sum(testScores)/len(testScores))
做
以决定的方式而不是
def determine_grade():
做
def calculate_average():
x=0
打印(“输入测试分数”)
(x=90,avg=80,avg=70,avg=60,avg,你必须回去学习C++和C。)在这些语言中使用全局变量是不好的练习,局部变量是非常有用的。好了,原谅我的无知,我知道它在做什么,但是这怎么更好呢?方法?您现在又创建了两个变量,而不是仅仅拥有一个变量“avg”(通过使其成为全局变量),并允许函数调用它并在必要时由它们进行更新?看起来您所做的一切(以及整个方法)创造了更大的复杂性,这就允许了更高的误会几率。汉克这样做很有魅力,但我正在努力解决的是为什么他们这么做。为什么这种方式被认为比简单地拥有一个全局变量更好。很明显,我是如何让它工作得很好,而不是必须创建多个变量实例。T这是一个更好的方法。首先,这更容易理解局部变量发生了什么,因为你不必跟踪它的所有使用情况,这会以不寻常的方式改变它的状态。这是一个管理程序复杂性的好方法。第二,这会导致紧密耦合,重构你的程序变得非常困难当你想创建一个独立的模块甚至函数时,你可以编写代码。当你不需要一步一步地通过所有的程序来理解变量为什么会得到错误的值时,调试也更简单。
return float(sum(testScores)/len(testScores))
def determine_grade():
def determine_grade(avg):
def calculate_average():
x = 0
print("Enter test scores")
while (x <=4):
test = input('Enter test score: ')
x += 1
testScores.append(float(test))
time.sleep(1)
print("","calculating average", ".........", "", sep = "\n")
time.sleep(1)
avg = float(sum(testScores)/len(testScores))
print("%.2f" % avg)
return avg
def determine_grade(avg):
if (avg >= 90.0 and avg <= 100.0):
print("Grade = A")
elif (avg >= 80.0 and avg <= 89.9):
print("Grade = B")
elif (avg >= 70.0 and avg <= 79.9):
print("Grade = C")
elif (avg >= 60.0 and avg <= 69.9):
print("Grade = D")
elif (avg <= 59.9):
print("Grade = F")
def main():
print("Beginning Program","", sep = "\n")
time.sleep(1)
avg = calculate_average()
determine_grade(avg)
time.sleep(1)
print("","End of program...", "Have a good day...", "Program Terminated", sep = "\n")