简体   繁体   中英

Simple quiz function on python

I am making a Spongebob quiz on Python, and I am running into some problems. My plan is to add up the number of times each answer was chosen, and based on which sum is the greatest, display a result. When I tested to see if it is working, after selecting two A answers, the sum of the Ax list shows 1 instead of 2. For some reason, the count_answers function is not taking into account previous inputs. Any ideas on how to fix this? I am a really beginner coder, and I would appreciate any help. Thanks!

def spongebob_quiz():

    print "Which character from Spongebob are you?"

    print "Question 1: Pick a word"
    print "A. Wumbo"
    print "B. CHOCOLATE"
    print "C. Weast"
    print "D. Hoopla"

    count_answers()

    print "Question 2: Is mayonnaise an instrument?"
    print "A. CORRECT!"
    print "B. Yes"
    print "C. No"
    print "D. Maybe"

    count_answers()


def count_answers():
    Ax=[]
    Bx=[]
    Cx=[]
    Dx=[]

    input=raw_input("Answer: ")

    if input=='A':
        Ax+=[1]
    if input=='B':
        Bx+=[1]
    if input=='C':
        Cx+=[1]
    if input=='D':
        Dx+=[1]

You have your lists Ax etc. initialised blank inside the function.

This means everytime you're calling the function, the lists are being re-instantiated blank.

You need to move the lists out of the function, see here:

Ax=[]
Bx=[]
Cx=[]
Dx=[]

def spongebob_quiz():

    print "Which character from Spongebob are you?"

    print "Question 1: Pick a word"
    print "A. Wumbo"
    print "B. CHOCOLATE"
    print "C. Weast"
    print "D. Hoopla"

    count_answers()

    print "Question 2: Is mayonnaise an instrument?"
    print "A. CORRECT!"
    print "B. Yes"
    print "C. No"
    print "D. Maybe"

    count_answers()


def count_answers():

    input=raw_input("Answer: ")

    if input=='A':
        Ax+=[1]
    if input=='B':
        Bx+=[1]
    if input=='C':
        Cx+=[1]
    if input=='D':
        Dx+=[1]

This should work fine.

Because Ax, Bx and so forth are local variables to your function, you basically get a fresh set of variables every time you call the function.

As a dirty trick, you could declare them as global variables.

The problem is that the variables you define inside count_answers ( Ax , Bx , Cx , Dx ) don't persist across different calls to the function.

In other words, every time you call the function, the variables get instantiated, ie - new lists are being created.

This isn't specific to python - it happens in most of the programing language.

You can better understand it intuitively by unfolding the calls to the function: just mentally replace every call to the function with the function's code, and you get that the variables get initialized twice, and their state isn't preserved - the values don't add up.

To solve this issue, you need to make sure you create your variables only once. One way to do so it to create the variables inside spongebob_quiz, and pass them as parameters to count_answers. count_answers in return will only update the variables. It won't initialize them. They'll get initialized only once.

PS:

If you want to be more precise, what's really happening is that once a function's execution ends, all the variables defined inside it are being deleted. There's no way you can access them (unless you use more advanced paradigms such as closures. But let's keep it simple :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM