#๐Ÿ”’ creating a new variable changes my previous variable and I'm not sure why

44 messages ยท Page 1 of 1 (latest)

toxic horizon
#

https://paste.pythondiscord.com/SZFA

Two things that are wrong with what's happening:
First, the window.close() method never seems to get called until both windows have had submit hit. not sure why.
Second, after the second window, it seems to overwrite the PossibleAnswers dictionary in the first question with the dictionary from the second. I have no clue why. I store it as a part of a testQuestion object, which shouldn't be modified anymore after the first instance of createQuestionGUI. I know its a big project, I appreciate everyone who gives me suggestions!

vocal quailBOT
#

@toxic horizon

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

toxic horizon
#

oh, no error messages get thrown. Just that the dictionary gets overwritten when I dont expect it to.

umbral fjord
#

It's probably because you're defaulting a variable to a mutable type

toxic horizon
#

what does that mean?

umbral fjord
#

Instead of this

    def __init__(self, questionText, multipleChoice, correctAnswer,possibleAnswers={}):
        self.multipleChoice = multipleChoice
        self.questionText = questionText
        self.correctAnswer = correctAnswer
        self.possibleAnswers = possibleAnswers

You need to do something like this

    def __init__(self, questionText, multipleChoice, correctAnswer,possibleAnswers=None):
        self.multipleChoice = multipleChoice
        self.questionText = questionText
        self.correctAnswer = correctAnswer
        if possibleAnswers is None:
            possibleAnswers = {}
        self.possibleAnswers = possibleAnswers
umbral fjord
#

watch what happens...

toxic horizon
#

okay let me try it

umbral fjord
#

!e

def foo(a=[]):
  a.append(1)
  print(a)

foo()
foo()
foo()
vocal quailBOT
#

@umbral fjord :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | [1]
002 | [1, 1]
003 | [1, 1, 1]
toxic horizon
#

that didn't solve the problem

#

the correctAnswer dictionary still gets overrwitten

#

when I print out the first one I get True who did it a {'a': 'me', 'b': 'you'}
When I print out both immediately afterwards, I get True, who did it, a, {'a': 'here', 'b': 'there', 'c': 'nowhere', 'd': 'everywhere'} True, where am I, c, {'a': 'here', 'b': 'there', 'c': 'nowhere', 'd': 'everywhere'}

#

same thing

toxic horizon
#

one sec

#

okay well now it's different.
https://paste.pythondiscord.com/QX5Q

When it runs the first time, it returns True, yes or no, a, {'a': 'yes', 'b': 'no'}. But the second time it runs and prints both it prints True, yes or no, a, {'a': 'true', 'b': 'no'} True, true or false, a, {'a': 'true', 'b': 'no'}.
So the first entry never gets overwritten and the second always does???

#

the adjustments at the top are to rejigger how it outputs to the file. no real changes beyond the class changes suggested above.

umbral fjord
toxic horizon
#

Yes

umbral fjord
#
    var = createQuestionGUI()
    print(var)
    var2 = createQuestionGUI(var)

Why does the first question get passed into the function to create the second question?

toxic horizon
#

The whole point of the project is that you can make a question or you can take one that already exists and edit it. It does that to test if that works and that's what doesn't work

umbral fjord
#

ok i see

#

But the second time it runs and prints both

It seems like this is what you're telling it to do though with

    print(var,var2)
toxic horizon
#

yes thats what I want, but the problem is that they both have the same dict for possibleAnswers, which isn't what I want

supple spade
#

There's a bit to read through here, but createQuestionGUI is just taking the possible_answers from the first object then giving it to the next.

toxic horizon
#

when I create a question with two answers 'true' and 'false', then edit it so the answers are 'yes' and 'no, the end result is that both questions end up with the dict being 'true' and 'no' which is so weird

supple spade
#

And note that this will never close the window since return is coming first:

return testQuestion(questionText, MultipleChoice, str(selectedAnswer), possibleAnswers)
window.close()
umbral fjord
#

And here

        for _ in range(QCountInput):
            if ev1 == f'PossibleAnswersText{_+1}':
                possibleAnswers.update({translator.get(_+1): val1[f'PossibleAnswersText{_+1}']})
                print(possibleAnswers)

You do an update() which will change every reference to this dictionary

#

including the original one in the first testQuestion that you make

toxic horizon
#

how could I change that

umbral fjord
#

you can use copy.deepcopy

        possibleAnswers = deepcopy(arg.getPA())
#

i guess deepcopy isnt needed if possibleAnswers is never a nested dict. you could just do this instead

possibleAnswers = arg.getPA().copy()
toxic horizon
#

okay let me try that

#

okay it almost worked. The new issue is that the number of answers doesn't seem to be held across properly

#

when I run it and don't touch the combo dropdown box, it breaks and only updates the key 'a' in the dictionary. But when I change the number in the box or even just click on two again, the whole thing fixes itself and I can update correctAnswer and the dictionary tracks properly.

#

I really appreciate everyone's help on this. This is the senior project for my undergrad degree and im in the home stretch! (not in computer science thank god haha)

#

I figured it out! I created an init in my while True loop that updates the visibility correctly if the question came from an input.

#

Thanks all!!

#

!close

vocal quailBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.