簡體   English   中英

將變量中的數據從一個函數傳遞到另一個函數

[英]Passing data in a variable from one function to another

我正在嘗試開發解決方案,以解決我的學生必須完成的課程任務,並且無法在函數之間傳遞變量。 我已經在生成以下數據的函數中創建了一個測驗:

  • 名字
  • 得分

我需要將這些數據傳遞給第二個函數,然后將其附加到文件中(取決於學生所在的組)。

我的代碼如下所示,因為我試圖將數據作為字符串/列表傳遞給第二個函數作為參數:

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 #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進行score

代替

return result
#?passes the data back?
funcSave()

funcSave(result)

另外,將您的Quiz函數重命名為funcQuiz ,以便重啟。

我認為您在將數據傳遞給funcSave函數之前正在返回數據。 當您想將數據傳遞給另一個函數中的一個函數時,您不想返回數據。 返回數據使您可以從函數中獲取數據,但是它也結束了函數的執行。

嘗試這個:

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")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM