Hello, I'm a beginner who started studying coding with Python. I think it will be easy to solve if you process it with a for statement, but I'm not used to it yet, so I'm leaving a question. In the code below, I want to change the part to 2 and 3 and 4 with the number 1.
if Prescription_1 == 'Heating':
if PreStateBoiler_1 == 'Boiler_On_1' and CurrBoiState_1 == 0:
cur.execute(sExecQuery, Boiler_On_1())
conn.commit()
PreStateBoiler_1= 'Boiler_On_1'
print('(Room 1) Status: Heating Zone, Error: Boiler restart')
log.write(' (Room 1) Status: Heating Zone, Error: Boiler restart, ')
elif PreStateBoiler_1 == 'Boiler_On_1' and CurrBoiState_1 == 1:
print('(Room 1) Status: Heating Zone, Boiler is working')
log.write('(Room 1) Status: Heating Zone, Boiler is working, ')
elif PreStateBoiler_1 == 'Boiler_Off_1' and CurrBoiState_1 == 0:
cur.execute(sExecQuery, Boiler_On_1())
conn.commit()
PreStateBoiler_1= 'Boiler_On_1'
print('(Room 1) Status: Heating Zone, Boiler start operation')
log.write(' (Room 1) Status: Heating Zone, Boiler start operation, ')
elif PreStateBoiler_1 == 'Boiler_Off_1' and CurrBoiState_1 == 1:
PreStateBoiler_1= 'Boiler_On_1'
print('(Room 1) Status: Heating Zone, Boiler is working')
log.write('(Room 1) Status: Heating Zone, Boiler is working, ')
else:
print('(Room 1) Status: Heating Zone, Unknown ')
log.write('(Room 1) Status: Heating Zone, Unknown, ')
To solve this problem, I thought it would be good to use the For statement. I did coding like below.First, I applied the string variable to the for statement. It does not run itself. Did you put the string variable wrong? Please give me some advice.
for k in range (1,5):
if 'Prescription_%d' %k == 'Heating':
if 'PreStateBoiler_%d' %k == "'Boiler_On_%d'%k" and 'CurrBoiState_%d'%k == 0:
cur.execute(sExecQuery, 'Boiler_On_%d()'%k)
conn.commit()
'PreStateBoiler_%d' %k = "'Boiler_On_%d'%k"
print("'(Room %d) Status: Heating Zone, Error: Boiler restart'%k")
log.write("'(Room %d) Status: Heating Zone, Error: Boiler restart, '%k")
elif 'PreStateBoiler_%d'%k == "'Boiler_On_%d'%k" and 'CurrBoiState_%d'%k == 1:
print("'(Room %d) Status: Heating Zone, Boiler is working'%k")
log.write("'(Room %d) Status: Heating Zone, Boiler is working, %k'")
elif 'PreStateBoiler_%d' %k == "'Boiler_Off_%d'%k" and 'CurrBoiState_%d'%k == 0:
cur.execute(sExecQuery, 'Boiler_On_%d'%k())
conn.commit()
'PreStateBoiler_%d' %k= "'Boiler_On_%d'%k"
print("'(Room %d) Status: Heating Zone, Boiler start operation'%k")
log.write("'(Room %d) Status: Heating Zone, Boiler start operation, '%k")
elif 'PreStateBoiler_%d' %k == "'Boiler_Off_%d'%k" and 'CurrBoiState_%d'%k == 1:
'PreStateBoiler_%d'%k= "'Boiler_On_%d'%k"
print("'(Room %d) Status: Heating Zone, Boiler is working'%k")
log.write("'(Room %d) Status: Heating Zone, Boiler is working, '%k")
else:
print("'(Room %d) Status: Heating Zone, Unknown!! '%k")
log.write("'(Room %d) Status: Heating Zone, Unknown!!, '%k")
I don't know what you want specifically, but maybe you want this form?
for i in range(0,5):
exec('a%d = %d' % (i,i))
a1
a2
a3
a4
a5
Please apply it;
© 2024 OneMinuteCode. All rights reserved.