O
O
oledjigames2020-11-02 23:05:50
Python
oledjigames, 2020-11-02 23:05:50

Python variable in class is not assigned to global, what's wrong?

When self.choice_var = (0, 1) or (1, 0) occurs but choice_1 does not change to (0, 1) or (1, 0) but remains the original choice_1 = 0, 0 why?

choice_1 = (0, 0)

class choice:
def __init__(self,level_num, text1, text2, choice_var, background, player, player_x, player1, player1_x):
     self.level_num = level_num
     self.text1 = text1
     self.text2 = text2
     self.choice_var = choice_var
     self.background = background
     self.player = player
     self.player_x = player_x
     self.player1 = player1
     self.player1_x = player1_x
     self.show()
def show(self):
    if level_selected==(self.level_num):
        screen.blit(self.background,(0, 0))
        screen.blit(self.player, (self.player_x, 0))
        screen.blit(self.player1, (self.player1_x, 0))
        screen.blit(choice_pic,(0, 0))
        if mousey<=(295):
            text_choice_1 = font1.render(self.text1, 1, YELLOW)#РАБОТАЕТ
            text_choice_2 = font1.render(self.text2, 1, WHITE)#РАБОТАЕТ
            self.choice_var = (1, 0)# ВОТ ЗДЕСЬ ЭТО НЕ РАБОТАЕТ
        elif mousey>=(295):
            text_choice_2 = font1.render(self.text2, 1, YELLOW)#РАБОТАЕТ
            text_choice_1 = font1.render(self.text1, 1, WHITE)#РАБОТАЕТ
            self.choice_var = (0, 1) # ВОТ ЗДЕСЬ ЭТО НЕ РАБОТАЕТ
        screen.blit(text_choice_1,(452, 233))
        screen.blit(text_choice_2,(452, 320))
        pygame.display.update()
    pass

And in the loop the line that calls this function
dialog1 = choice(9, "yes", "no", choice_1, background2, player0, 120, player1, -120)
    dialog1.show()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sswwssww, 2020-11-03
@oledjigames

choice_1 = (0, 0)
- choice_1 is a tuple .
In Python , tuple is an immutable data type, you probably need a list data type , it is mutable ( choice_1 = [0, 0], and accordingly in the class also replace () with [] where necessary ). In general, it’s strange that you somehow study this PL, skipping the basics of the language and immediately rushed to the classes.

G
grieverrr, 2020-11-03
@grieverrr

and what prompted you to use the immutable tuple type for the variable you intend to change?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question