将变量中的数据从一个函数传递到另一个函数
Passing data in a variable from one function to another
我正在尝试为我的学生必须完成的课程任务开发一个解决方案,但我一直坚持在函数之间传递变量。我在生成以下数据的函数中创建了一个测验:
- 名字
- 姓氏
- 得分
我需要将此数据传递到第二个函数,然后将此数据附加到文件(取决于学生所在的组)。
我的代码看起来像这样,因为我试图将数据作为 string/list 传递到第二个函数作为参数:
def Quiz():
#Code to collect user details and generate scoe (10 random questions)
strSave=input("Do you want to save your score? y/n")
if strSave.lower()=="y":
result=(strFirstName+","+strLastName+","+str(score))
#Stores the required data as a csv string
return result
#?passes the data back?
funcSave()
elif strSave.lower()=="n":
funcQuiz()
#restarts the quiz function
def funcSave(result):
#The function to save the score(s) to file
group=input("Which group are you A1, A2 or A3?\n")
if group=="A1":
file=open("A1.txt","a")
file.write(result)
#error is that result is not defined
file.close()
elif group=="A2":
file=open("A2.txt","a")
file.write(strFirstName+","+strLastName+","+str(score))
file.close()
elif group=="A3":
file=open("A3.txt","a")
file.write(strFirstName+","+strLastName+","+str(score))
file.close()
else:
print("unknown")
而不是
return result
#?passes the data back?
funcSave()
做
funcSave(result)
此外,将您的 Quiz
函数重命名为 funcQuiz
,以便重新启动。
我认为您在将数据传递给 funcSave 函数之前 return 正在处理数据。当您想将数据传递给另一个函数中的一个函数时,您不需要 return 数据。返回数据允许您从函数中获取数据,但它也会结束函数的执行。
试试这个:
def Quiz():
#Code to collect user details and generate scoe (10 random questions)
strSave=input("Do you want to save your score? y/n")
if strSave.lower()=="y":
result=(strFirstName+","+strLastName+","+str(score))
# Pass the result to the funcSave function instead of returning it.
funcSave(result)
elif strSave.lower()=="n":
# rename the funcQuiz() function to Quiz() so it is called correctly
Quiz()
#restarts the quiz function
def funcSave(result):
#The function to save the score(s) to file
group=input("Which group are you A1, A2 or A3?\n")
if group=="A1":
file=open("A1.txt","a")
file.write(result)
#error is that result is not defined
file.close()
elif group=="A2":
file=open("A2.txt","a")
file.write(strFirstName+","+strLastName+","+str(score))
file.close()
elif group=="A3":
file=open("A3.txt","a")
file.write(strFirstName+","+strLastName+","+str(score))
file.close()
else:
print("unknown")
你的问题在这里:
return result #immediately ends the method, returning result to the caller
funcSave() # Is never executed because you've return'd. Would throw TypeError if it did because funcSave() needs one argument
您需要删除 return
调用,然后从 Quiz
方法实际传递 results
变量,如下所示:
funcSave(results)
您在 Quiz
中也有错字,它调用 funcQuiz()
而不是 Quiz()
来重新启动。
顺便说一句,而不是这个:
result=(strFirstName+","+strLastName+","+str(score))
你可以这样做:
result = ','.join((strFirstName,strLastName,str(score)))
python 中的 join
方法使用 .
之前的字符串作为分隔符将值列表连接在一起。它比使用 +
更有效,因为 python 不需要创建任何中间字符串。请注意 join
期望所有值都是字符串,因此您仍然需要对 score
.
进行强制转换
我正在尝试为我的学生必须完成的课程任务开发一个解决方案,但我一直坚持在函数之间传递变量。我在生成以下数据的函数中创建了一个测验:
- 名字
- 姓氏
- 得分
我需要将此数据传递到第二个函数,然后将此数据附加到文件(取决于学生所在的组)。
我的代码看起来像这样,因为我试图将数据作为 string/list 传递到第二个函数作为参数:
def Quiz():
#Code to collect user details and generate scoe (10 random questions)
strSave=input("Do you want to save your score? y/n")
if strSave.lower()=="y":
result=(strFirstName+","+strLastName+","+str(score))
#Stores the required data as a csv string
return result
#?passes the data back?
funcSave()
elif strSave.lower()=="n":
funcQuiz()
#restarts the quiz function
def funcSave(result):
#The function to save the score(s) to file
group=input("Which group are you A1, A2 or A3?\n")
if group=="A1":
file=open("A1.txt","a")
file.write(result)
#error is that result is not defined
file.close()
elif group=="A2":
file=open("A2.txt","a")
file.write(strFirstName+","+strLastName+","+str(score))
file.close()
elif group=="A3":
file=open("A3.txt","a")
file.write(strFirstName+","+strLastName+","+str(score))
file.close()
else:
print("unknown")
而不是
return result
#?passes the data back?
funcSave()
做
funcSave(result)
此外,将您的 Quiz
函数重命名为 funcQuiz
,以便重新启动。
我认为您在将数据传递给 funcSave 函数之前 return 正在处理数据。当您想将数据传递给另一个函数中的一个函数时,您不需要 return 数据。返回数据允许您从函数中获取数据,但它也会结束函数的执行。
试试这个:
def Quiz():
#Code to collect user details and generate scoe (10 random questions)
strSave=input("Do you want to save your score? y/n")
if strSave.lower()=="y":
result=(strFirstName+","+strLastName+","+str(score))
# Pass the result to the funcSave function instead of returning it.
funcSave(result)
elif strSave.lower()=="n":
# rename the funcQuiz() function to Quiz() so it is called correctly
Quiz()
#restarts the quiz function
def funcSave(result):
#The function to save the score(s) to file
group=input("Which group are you A1, A2 or A3?\n")
if group=="A1":
file=open("A1.txt","a")
file.write(result)
#error is that result is not defined
file.close()
elif group=="A2":
file=open("A2.txt","a")
file.write(strFirstName+","+strLastName+","+str(score))
file.close()
elif group=="A3":
file=open("A3.txt","a")
file.write(strFirstName+","+strLastName+","+str(score))
file.close()
else:
print("unknown")
你的问题在这里:
return result #immediately ends the method, returning result to the caller
funcSave() # Is never executed because you've return'd. Would throw TypeError if it did because funcSave() needs one argument
您需要删除 return
调用,然后从 Quiz
方法实际传递 results
变量,如下所示:
funcSave(results)
您在 Quiz
中也有错字,它调用 funcQuiz()
而不是 Quiz()
来重新启动。
顺便说一句,而不是这个:
result=(strFirstName+","+strLastName+","+str(score))
你可以这样做:
result = ','.join((strFirstName,strLastName,str(score)))
python 中的 join
方法使用 .
之前的字符串作为分隔符将值列表连接在一起。它比使用 +
更有效,因为 python 不需要创建任何中间字符串。请注意 join
期望所有值都是字符串,因此您仍然需要对 score
.