Passing data in a variable from one function to another

I'm trying to develop a solution to a coursework task my pupils have to complete and am stuck with passing variables between functions. I have created a quiz within a function that generates the following data:

I need to pass this data into a second function that will then append this data to a file (depending on the group the student is in).

My code looks like this as I have tried to pass the data as a string/list into the second function as a parameter:

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

Upvotes: 0

Views: 98

Answers (3)

aruisdante
aruisdante

Reputation: 9115

Your problem is here:

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

You need to remove the return call, then actually pass the results variable from the Quiz method, like so:

funcSave(results)

You also have a typo in Quiz, where it calls funcQuiz() instead of Quiz() to restart.

As an aside, instead of this:

result=(strFirstName+","+strLastName+","+str(score))

You can just do this:

result = ','.join((strFirstName,strLastName,str(score)))

The join method in python concatenates a list of values together using the string before the . as the delimiter. It's more efficient than using + as python doesn't need to create any intermediary strings. Note that join expects all values to be strings, so you still need the cast on score.

Upvotes: 2

RattleyCooper
RattleyCooper

Reputation: 5207

I think you are returning the data before you pass it to the funcSave function. When you want to pass data to a function within another function, you don't want return the data. Returning data allows you to get data out of a function, but it ends the execution of the function as well.

Try this:

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

Upvotes: 0

L3viathan
L3viathan

Reputation: 27331

Instead of

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

do

funcSave(result)

Also, rename your Quiz function to funcQuiz, so the restart works.

Upvotes: 0

Related Questions