Converting a string and a int into a variable Python

230 views Asked by At

I am currently creating a loop with sikuli. My problem is that i have a fixed variables that will go up to 15 with only the numbers changing at the end. I was looking for a way to combine the string component that is fixed with the integer which will be variable in the loop but then once concatenated have it identified as the predefined variable at the top of the code.

Any help would be awesome!

Dunning1 = (Pattern("Line 1.png").similar(0.97).targetOffset(445,-2))
Balance1 = (Pattern("Line 1.png").similar(0.97).targetOffset(566,-2)) 
Select1 = (Pattern("Line 1.png").similar(0.97).targetOffset(38,-1))
Dunning2 = (Pattern("Line 2.png").similar(0.97).targetOffset(442,-1))
Balance2 =(Pattern("Line 2.png").similar(0.97).targetOffset(565,2))
Select2 = (Pattern("Line 2.png").similar(0.97).targetOffset(37,-1))

while n < 3:
    DunningX = ("Dunning"+str(n)**
    print(DunningX)**
    doubleClick(DunningX)
    type("c",KEY_CTRL)
    doubleClick(DunningX)
    type("c",KEY_CTRL)
    Dunning1 = Env.getClipboard()
    BalanceX = ("Balance"+str(n))  
    doubleClick(BalanceX)
    type("c",KEY_CTRL)
    doubleClick(BalanceX)
    type("c",KEY_CTRL)
    ContractBal = Env.getClipboard()
    if Dunning1 == ContractBal:
        SelectX = ("Select"+str(n))  
        click(SelectX)
    n = n + 1
3

There are 3 answers

0
Gamopo On

I'm not sure if I fully understand your question, but I think you're looking for this:

if some_condition:
    Select1 = "Select"+str(n)
else
    Select2 = "Select"+str(n)

any way, please consider using a list for this since using single variables is not scalable at all. It could look like this:

select = []
select.append(Pattern("Line 1.png").similar(0.97).targetOffset(38,-1))
select.append(Pattern("Line 2.png").similar(0.97).targetOffset(37,-1))
...
if some_condition:
    m=1
else
    m=2
select[m] = 'select'+str(n)
0
Ben Woo On

From your code shown i see a few issues.

  1. I assume your while n < 3 is indentation issue.

  2. error with (:

DunningX = "Dunning" + str(n)
print(DunningX)
0
Gabio On

I would recommend you to do the following:

1 - add all the variables to a class as attributes:

class Variables:
    def __init__(self):
        self.Dunning1 = (Pattern("Line 1.png").similar(0.97).targetOffset(445,-2))
        self.Balance1 = (Pattern("Line 1.png").similar(0.97).targetOffset(566,-2)) 

2 - get the values dynamically by their name using getattr func:

n=1 // for example
vars = Variables()
DunningX = getattr(vars,f"Dunning{n}") //DunningX will be equal to Dunning1