user3415453
user3415453

Reputation: 39

Importing variable not giving the desired value

It this game the score is set you 0 at the beginning and added to if you answer a question correctly, If i check the score by printing while I'm playing it gives the correct value but when the score is shown on the certificate it displays 0 no matter what.

Here's the code for the first file (The game part)

import pygame, time
pygame.init()
WHITE = ( 255, 255, 255)
BLACK = ( 0, 0, 0)
screen = pygame.display.set_mode((600, 600))
ButtonA = pygame.image.load("A.png")
ButtonB = pygame.image.load("B.png")
ButtonC = pygame.image.load("C.png")
a = screen.blit(ButtonA,(50,400))
b = screen.blit(ButtonB,(250,400))
c = screen.blit(ButtonC,(450,400))
pygame.display.flip()

score = 0
if __name__ == '__main__':
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            pos = pygame.mouse.get_pos()
            if a.collidepoint(pos):
                screen.fill(BLACK)
                Correct = font.render("You are correct!", True, WHITE)
                Correct_rect = Correct.get_rect()
                Correct_x = screen.get_width() / 2 - Correct_rect.width / 2
                Correct_y = 150 - Correct_rect.height / 2
                screen.blit(Correct, [Correct_x, Correct_y])
                pygame.display.flip()
                score = score + 1
                print score
                time.sleep(2)

And here is the code for the part where it puts the value on a certificate which currently throws up nothing but the number 0

import pygame, sys, eztext
from pygame.locals import *
from eztext import Input
#Importing the variable score
from Question1Easy import score
Name = self.value
#Setting the variable score (an int) to a string
Score = str(score)

How do I get the second file to grab the updated value of score instead of the static 0?

Upvotes: 1

Views: 84

Answers (2)

Kyle Me
Kyle Me

Reputation: 336

So what I think you should do is make a class. A class might sound like a bit of work, but it's definitely worth it to get used to them, and using a class in this way is very easy.

#players.py 
class Player():
    def __init__(self):
        self.score = 0

#games.py
def checkers(player):
    #blahblahblah dostuff
    player.score += 1
    print(player.score) #if everything goes right, this should print 1

#main.py
from players import player
from games import checkers
player = Player()
checkers(player)
player.score += 1
print(player.score) #if everything goes right, this should print 2

The downside to using this method, is for each function that you want to go to, I think you have to pass player as an argument for almost every function, which can get a bit tedious. However, player can hold multiple variables or other stats that you want to keep track of. You only need one player variable (now object), just in the initializer put more variables.

When I was having a similar issue to you, I found this to be the easiest solution.

Upvotes: 2

WKPlus
WKPlus

Reputation: 7255

Yes, it will certainly give you 0.

When imported by another file, the code under if __name__ == '__main__' will not be executed.

You can put the logic under if __name__ == '__main__' into a function in the first py file, for example function start_game. And then in the second py file call the start_game function to let the game run. Besides this, you need to add a function to return the score in the first file like this:

First file:

score = 0
def start_game():
    global score
    score += 1

def get_score():
    return score

Second file:

from firstfile import get_score, start_game
print get_score()
start_game()
print get_score()

When you run the second file, you will get:

0
1

You get a static value by using from firstfile import score, if you want to get the dynamic value, you need to add a function(get_score in the above code).

Upvotes: 2

Related Questions