Python Forum
Calling on a Variable using other Variables
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calling on a Variable using other Variables
#1
Question 
Is it possible to call upon a variable using other variables? For example if you have the name of a variable stored in another variable, could you call upon the first variable using the name of the variable stored in the other variable?

# The original variable
var1 = 'Z26'

var2 = 'Z'
var3 = '26'

# Is there a way to make this be equivalent to var1 = 'A1'?
var2 + var3 = 'A1'
Thanks!
Reply
#2
It appears you want var1 to be the concatination of var2 and var3 without using any code. You are going to need some code. What are you trying to do?
Reply
#3
I'm trying to make a chess game that will highlight the squares that the piece can play in, but to do that I need to change a variable that stores the color of the square using a variable. Here is the start of my code:

import pygame

pygame.init()
screen = pygame.display.set_mode((800,800))

pygame.display.set_caption('Chess')

x = 425
y = 325
h1 = [0,0,120]
g1 = [0,0,0]
f1 = [0,0,120]
e1 = [0,0,0]
d1 = [0,0,120]
c1 = [0,0,0]
b1 = [0,0,120]
a1 = [0,0,0]
h2 = [0,0,0]
g2 = [0,0,120]
f2 = [0,0,0]
e2 = [0,0,120]
d2 = [0,0,0]
c2 = [0,0,120]
b2 = [0,0,0]
a2 = [0,0,120]
h3 = [0,0,120]
g3 = [0,0,0]
f3 = [0,0,120]
e3 = [0,0,0]
d3 = [0,0,120]
c3 = [0,0,0]
b3 = [0,0,120]
a3 = [0,0,0]
h4 = [0,0,0]
g4 = [0,0,120]
f4 = [0,0,0]
e4 = [0,0,120]
d4 = [0,0,0]
c4 = [0,0,120]
b4 = [0,0,0]
a4 = [0,0,120]
h5 = [0,0,120]
g5 = [0,0,0]
f5 = [0,0,120]
e5 = [0,0,0]
d5 = [0,0,120]
c5 = [0,0,0]
b5 = [0,0,120]
a5 = [0,0,0]
h6 = [0,0,120]
g6 = [0,0,0]
f6 = [0,0,120]
e6 = [0,0,0]
d6 = [0,0,120]
c6 = [0,0,0]
b6 = [0,0,120]
a6 = [0,0,0]
h7 = [0,0,120]
g7 = [0,0,0]
f7 = [0,0,120]
e7 = [0,0,0]
d7 = [0,0,120]
c7 = [0,0,0]
b7 = [0,0,120]
a7 = [0,0,0]
h8 = [0,0,120]
g8 = [0,0,0]
f8 = [0,0,120]
e8 = [0,0,0]
d8 = [0,0,120]
c8 = [0,0,0]
b8 = [0,0,120]
a8 = [0,0,0]


running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    
    h1square = pygame.draw.rect(screen, (h1), pygame.Rect(0, 0, 100, 100))
    g1square = pygame.draw.rect(screen, (g1), pygame.Rect(100, 0, 100, 100))
    f1square = pygame.draw.rect(screen, (f1), pygame.Rect(200, 0, 100, 100))
    e1square = pygame.draw.rect(screen, (e1), pygame.Rect(300, 0, 100, 100))
    d1square = pygame.draw.rect(screen, (d1), pygame.Rect(400, 0, 100, 100))
    c1square = pygame.draw.rect(screen, (c1), pygame.Rect(500, 0, 100, 100))
    b1square = pygame.draw.rect(screen, (b1), pygame.Rect(600, 0, 100, 100))
    a1square = pygame.draw.rect(screen, (a1), pygame.Rect(700, 0, 100, 100))
    a2square = pygame.draw.rect(screen, (a2), pygame.Rect(700, 100, 100, 100))
    b2square = pygame.draw.rect(screen, (b2), pygame.Rect(600, 100, 100, 100))
    c2square = pygame.draw.rect(screen, (c2), pygame.Rect(500, 100, 100, 100))
    d2square = pygame.draw.rect(screen, (d2), pygame.Rect(400, 100, 100, 100))
    e2square = pygame.draw.rect(screen, (e2), pygame.Rect(300, 100, 100, 100))
    f2square = pygame.draw.rect(screen, (f2), pygame.Rect(200, 100, 100, 100))
    g2square = pygame.draw.rect(screen, (g2), pygame.Rect(100, 100, 100, 100))
    h2square = pygame.draw.rect(screen, (h2), pygame.Rect(0, 100, 100, 100))
    h3square = pygame.draw.rect(screen, (h3), pygame.Rect(0, 200, 100, 100))
    g3square = pygame.draw.rect(screen, (g3), pygame.Rect(100, 200, 100, 100))
    f3square = pygame.draw.rect(screen, (f3), pygame.Rect(200, 200, 100, 100))
    e3square = pygame.draw.rect(screen, (e3), pygame.Rect(300, 200, 100, 100))
    d3square = pygame.draw.rect(screen, (d3), pygame.Rect(400, 200, 100, 100))
    c3square = pygame.draw.rect(screen, (c3), pygame.Rect(500, 200, 100, 100))
    b3square = pygame.draw.rect(screen, (b3), pygame.Rect(600, 200, 100, 100))
    a3square = pygame.draw.rect(screen, (a3), pygame.Rect(700, 200, 100, 100))
    a4square = pygame.draw.rect(screen, (a4), pygame.Rect(700, 300, 100, 100))
    b4square = pygame.draw.rect(screen, (b4), pygame.Rect(600, 300, 100, 100))
    c4square = pygame.draw.rect(screen, (c4), pygame.Rect(500, 300, 100, 100))
    d4square = pygame.draw.rect(screen, (d4), pygame.Rect(400, 300, 100, 100))
    e4square = pygame.draw.rect(screen, (e4), pygame.Rect(300, 300, 100, 100))
    f4square = pygame.draw.rect(screen, (f4), pygame.Rect(200, 300, 100, 100))
    g4square = pygame.draw.rect(screen, (g4), pygame.Rect(100, 300, 100, 100))
    h4square = pygame.draw.rect(screen, (h4), pygame.Rect(0, 300, 100, 100))
    h5square = pygame.draw.rect(screen, (h5), pygame.Rect(0, 400, 100, 100))
    g5square = pygame.draw.rect(screen, (g5), pygame.Rect(100, 400, 100, 100))
    f5square = pygame.draw.rect(screen, (f5), pygame.Rect(200, 400, 100, 100))
    e5square = pygame.draw.rect(screen, (e5), pygame.Rect(300, 400, 100, 100))
    d5square = pygame.draw.rect(screen, (d5), pygame.Rect(400, 400, 100, 100))
    c5square = pygame.draw.rect(screen, (c5), pygame.Rect(500, 400, 100, 100))
    b5square = pygame.draw.rect(screen, (b5), pygame.Rect(600, 400, 100, 100))
    a5square = pygame.draw.rect(screen, (a5), pygame.Rect(700, 400, 100, 100))
    h6square = pygame.draw.rect(screen, (h6), pygame.Rect(700, 500, 100, 100))
    g6square = pygame.draw.rect(screen, (g6), pygame.Rect(600, 500, 100, 100))
    f6square = pygame.draw.rect(screen, (f6), pygame.Rect(500, 500, 100, 100))
    e6square = pygame.draw.rect(screen, (e6), pygame.Rect(400, 500, 100, 100))
    d6square = pygame.draw.rect(screen, (d6), pygame.Rect(300, 500, 100, 100))
    c6square = pygame.draw.rect(screen, (c6), pygame.Rect(200, 500, 100, 100))
    b6square = pygame.draw.rect(screen, (b6), pygame.Rect(100, 500, 100, 100))
    a6square = pygame.draw.rect(screen, (a6), pygame.Rect(0, 500, 100, 100))
    h7square = pygame.draw.rect(screen, (h7), pygame.Rect(0, 600, 100, 100))
    g7square = pygame.draw.rect(screen, (g7), pygame.Rect(100, 600, 100, 100))
    f7square = pygame.draw.rect(screen, (f7), pygame.Rect(200, 600, 100, 100))
    e7square = pygame.draw.rect(screen, (e7), pygame.Rect(300, 600, 100, 100))
    d7square = pygame.draw.rect(screen, (d7), pygame.Rect(400, 600, 100, 100))
    c7square = pygame.draw.rect(screen, (c7), pygame.Rect(500, 600, 100, 100))
    b7square = pygame.draw.rect(screen, (b7), pygame.Rect(600, 600, 100, 100))
    a7square = pygame.draw.rect(screen, (a7), pygame.Rect(700, 600, 100, 100))
    h8square = pygame.draw.rect(screen, (h8), pygame.Rect(700, 700, 100, 100))
    g8square = pygame.draw.rect(screen, (g8), pygame.Rect(600, 700, 100, 100))
    f8square = pygame.draw.rect(screen, (f8), pygame.Rect(500, 700, 100, 100))
    e8square = pygame.draw.rect(screen, (e8), pygame.Rect(400, 700, 100, 100))
    d8square = pygame.draw.rect(screen, (d8), pygame.Rect(300, 700, 100, 100))
    c8square = pygame.draw.rect(screen, (c8), pygame.Rect(200, 700, 100, 100))
    b8square = pygame.draw.rect(screen, (b8), pygame.Rect(100, 700, 100, 100))
    a8square = pygame.draw.rect(screen, (a8), pygame.Rect(0, 700, 100, 100))

    pawn1 = pygame.draw.rect(screen, (255,255,255), pygame.Rect(x, y, 50, 50))

    pos = pygame.mouse.get_pos()
    pressed1, pressed2, pressed3 = pygame.mouse.get_pressed()
    if pawn1.collidepoint(pos) and pressed1:
        coords = pygame.mouse.get_pos()
        xcoord = coords[0] / 100
        ycoord = coords[1] / 100
                
        if ycoord > 7:
            ycoord = "8"
        elif 6 < ycoord < 7:
            ycoord = "7"
        elif 5 < ycoord < 6:
            ycoord = "6"
        elif 4 < ycoord < 5:
            ycoord = "5"
        elif 3 < ycoord < 4:
            ycoord = "4"
        elif 2 < ycoord < 3:
            ycoord = "3"
        elif 1 < ycoord < 2:
            ycoord = "2"
        elif ycoord < 1:
            ycoord = "1"

        if xcoord > 7:
            xcoord = "a"
        elif 6 < xcoord < 7:
            xcoord = "b"
        elif 5 < xcoord < 6:
            xcoord = "c"
        elif 4 < xcoord < 5:
            xcoord = "d"
        elif 3 < xcoord < 4:
            xcoord = "e"
        elif 2 < xcoord < 3:
            xcoord = "f"
        elif 1 < xcoord < 2:
            xcoord = "g"
        elif xcoord < 1:
            xcoord = "h"

        upto = int(ycoord) + 1
        print(xcoord + str(upto))
        xcoord + str(upto) = var
        var = [255,255,255]
        
            
    pygame.display.update()
Using this code I can get the name of the square that the pawn is in, but to do anything with that I have to call upon a variable using this. Thanks!
Reply
#4
Instead of using a variable to store the color you could use a function that returns the color. As I said, somewhere you need code.

Your code is odd. I don't have any pygame experience at all, but I think you would want to some sort of collection object instead of having a named variable for each thing. Since the squares on the board are an important part of the game I might make a class to hold all the information for a square such as the location, the background color, and any chess piece landing on the square:
class Square():
    '''A location on the board'''
    def __init__(self, rank, file):
        self.rank = rank
        self.file = file
        self.name = 'abcdefgh'[rank]+str(file+1)
        self.background = [0,0,120] if (rank+file)%2 else [0,0,0]
        x = (rank)*100  # Not sure of pygame coordinates so this could be wrong
        y = (file)*100
        self.rectangle = [x, y, 100, 100]
        self.occupant = None

    def draw(self):
        '''Draw self on screen'''
        self.square = pygame.draw.rect(screen, self.color, self.rectangle)
        if self.occupant:
            '''Draw image for piece occupying square'''

    @property
    def piece(self):
        '''Return occupant'''
        return self.occupant

    @piece.setter
    def place(self, piece):
       '''Set the occupant.  Redraw the square'''
        self.occupant = piece
        self.draw()
In addition to storing information a class can also have actions. My Square class would have an action (method) to draw the square and another for adding or removing a chess piece. I might make the latter a property so I can use the same property to see or set what piece occupies a square.

The board is just a collection of squares, so I make a class that represents that.
class Board():
    '''A grid of Squares'''
    def __init__(self):
        self.ranks = []
        for rank in range(8):
            self.rank.append([Square(rank, file) for file in range(8)])

    def square(self, rank, file):
        """Return square at rank/file"""
        return self.rank[rank][file]

    def move(self, r1, f1, r2, f2):
        '''Move piece from r1, f1 to r2, f2'''
        self.square(r2, f2).piece = self.square(r1, f1).piece
        self.square(r1, f1).piece = None

    def draw(self):
        '''Draw the board'''
        for rank in self.ranks:
            for square in rank:
                square.draw()
Now I can make and draw a chess board in two lines:
board = Board()
board.draw()
And I can move a piece in one line
board.move(startRank, startFile, endRank, endFile)
Reply
#5
Thanks, I think I can get this working now!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Calling functions by making part of their name with variable crouzilles 4 746 Nov-02-2023, 12:25 PM
Last Post: noisefloor
  Calling a base class variable from an inherited class CompleteNewb 3 1,591 Jan-20-2022, 04:50 AM
Last Post: CompleteNewb
  Create new variable dependent on two existing variables JoeOpdenaker 6 2,931 Oct-25-2020, 02:15 PM
Last Post: jefsummers
  Print variable values from a list of variables xnightwingx 3 2,571 Sep-01-2020, 02:56 PM
Last Post: deanhystad
  Calling Variables from Other Files in Different Folders illmattic 14 5,386 Aug-01-2020, 07:02 PM
Last Post: deanhystad
  Function Recognises Variable Without Arguments Or Global Variable Calling. OJGeorge4 1 2,205 Apr-06-2020, 09:14 AM
Last Post: bowlofred
  calling a variable in a for loop in windows dwaynes 1 1,774 Apr-02-2020, 05:21 PM
Last Post: mcmxl22
  setting pythonpath variable in environment variables saisankalpj 3 3,425 Dec-05-2018, 10:33 PM
Last Post: Gribouillis
  2D Array/List OR using variables in other variable names? IAMK 4 3,794 Apr-16-2018, 09:09 PM
Last Post: IAMK
  calling an object variable in a dictionary sunhear 3 4,233 Dec-30-2016, 05:28 PM
Last Post: sunhear

Forum Jump:

User Panel Messages

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