Python Forum
I dont know where my local variable has gone
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I dont know where my local variable has gone
#3
Python does not pass variables by reference. When "Question_Load" passes "score" to Q1, Q1 is using a copy of "score". This local copy gets passed along to "Q_display" which get's its own local copy. When "Q_display" changes the value of score it only affects the local copy of score inside "Q_display".

It looks like you understand variable scope somewhat. By returning "score", "Q_display" is passing back the modified score, but "Q1" ignores the return value, and returns it's own local copy which is not modified. Back in "Question_Load" the return value is ignored again, and it's local copy of score is also unaltered.

The short answer to your question is that the score get's lost because you ignore changes to it. You could easily make your program work by paying attention to when the modified score is returned and passing the modified score back up the chain of function calls.

Hopefully you are beginning to see that writing a function for each question is a bad idea. Your program already has some abstraction in the Easy_Bank, Medium Bank, and Hard_Bank list of questions. But these should not be lists of functions. You should have one function that knows how to ask a question (just like you currently have one function that knows how to display the question and get the response). The questions should be data. Here is how I would write a console based version of your program. I have 1 function that knows how to ask a question, and the questions are represented as a named tuple. A regular tuple, list or even dictionary would work just as well. A round or quiz is a list of these structures:
from collections import namedtuple

Question = namedtuple('Question', 'prompt choices answer difficulty')

geography = [
    Question(prompt = 'What is the longest river in the northern hemisphere?',
             choices = ('A: Amazon', 'B: Misissippi', 'C: Yangtze', 'D: Volga'),
             answer = 'C', difficulty = 2),

    Question(prompt = 'What is the highest mountain in South America?',
             choices = ('A: Kangchenjunga', 'B: Aconcagua', 'C: Olympus Mons', 'D: K2'),
             answer = 'B', difficulty = 3)
]

def ask_question(question):
    score = 0
    print(question.prompt)
    for choice in question.choices:
        print('   ', choice)
    response = input('Your Response: ').upper()
    if response == question.answer:
        print('That is correct!')
        score = question.difficulty
    else:
        print('That is incorrect.  The correct answer is', question.answer)
    return score
 
def take_quiz(quiz):
    score = 0
    for question in quiz:
        score += ask_question(question)
        print('Score =', score, '\n\n')
    
take_quiz(geography)
Actually, this is not how I would write the program. To make the program more flexible and useful I would write the program to only know how to ask questions and keep score. The actual questions would be data imported into the program. It could be a database, or a spreadsheet, or just a file. Instead of defining the quiz inside the program I would write a function that knows how to import the data and turn it into a Question.
Reply


Messages In This Thread
RE: I dont know where my local variable has gone - by deanhystad - Mar-30-2020, 07:18 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  local variable 'spider_1_health' referenced before assignment Tbot1000 1 1,805 Sep-01-2020, 09:07 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020