• 0

Python Loop and change varable names


Question

Hi all have been playing with python but stuck on a problem here it is

#HIGH ALARMS
		ProbeMailed1 = int(time.time()) - MailSentProb1
		if settings["Probe1AlarmHi"] != "x" and ProbeMailed1 > int(settings["MailResend"]) and settings["Probe1"] != "":
			if float(Probe1) > float(settings["Probe1AlarmHi"]): 
				SendMail("Probe 1 High")
				MailSentProb1 = int(time.time())
				
		ProbeMailed2 = int(time.time()) - MailSentProb2
		if settings["Probe2AlarmHi"] != "x" and ProbeMailed2 > int(settings["MailResend"]) and settings["Probe2"] != "":
			if float(Probe2) > float(settings["Probe2AlarmHi"]):
				SendMail("Probe 2 High")
				MailSentProb2 = int(time.time())
				
		ProbeMailed2 = int(time.time()) - MailSentProb3
		if settings["Probe3AlarmHi"] != "x" and ProbeMailed3 > int(settings["MailResend"]) and settings["Probe3"] != "":
			if float(Probe3) > float(settings["Probe3AlarmHi"]): 
				SendMail("Probe 3 High")
				MailSentProb3 = int(time.time())
				
		ProbeMailed4 = int(time.time()) - MailSentProb4
		if settings["Probe4AlarmHi"] != "x" and ProbeMailed4 > int(settings["MailResend"]) and settings["Probe4"] != "":
			if float(Probe4) > float(settings["Probe4AlarmHi"]):
				SendMail("Probe 4 High")
				MailSentProb4 = int(time.time())
		
		ProbeMailed5 = int(time.time()) - MailSentProb5
		if settings["Probe5AlarmHi"] != "x" and ProbeMailed5 > int(settings["MailResend"]) and settings["Probe5"] != "":
			if float(Probe5) > float(settings["Probe5AlarmHi"]): 
				SendMail("Probe 5 High")
				MailSentProb5 = int(time.time())
				
		ProbeMailed6 = int(time.time()) - MailSentProb6
		if settings["Probe6AlarmHi"] != "x" and ProbeMailed6 > int(settings["MailResend"]) and settings["Probe6"] != "":
			if float(Probe6) > float(settings["Probe6AlarmHi"]):
				SendMail("Probe 6 High")
				MailSentProb6 = int(time.time())

I'm trying to shorten this code into a loop but really cant figure it out here is what i have

countX = 1
	while (countX < 6):
		ProbeMailed+countX = int(time.time()) - MailSentProb+countX
		if settings["Probe"+countX+"AlarmHi"] != "x" and ProbeMailed+countX > int(settings["MailResend"]) and settings["Probe"+countX] != "":
			if float(Probe5) > float(settings["Probe"+countX+"AlarmHi"]): 
				SendMail("Probe "+countX+" High")
				MailSentProb+countX = int(time.time())
			countX = countX + 1

but this throughs up LOADS of errors

 

PLEASE HELP

5 answers to this question

Recommended Posts

  • 0

You cannot express a variable name by doing something like

variableName + x

where x is an integer. This expression is the addition of those variables' values, not a new variable name. Obviously such an expression cannot be on the left side of an assignment, which is what Python is complaining about.

 

In order to use a loop you'll have to store your data into a single collection rather than individual variables, which you can then index by using your countX variable. The most basic collections in Python are called lists.

 

More specifically, you want to define your ProbeMailed and MailSentProb variables as lists, and index them with the countX variable on each iteration of the loop.

  • Like 1
  • 0

ok so i'm a bit further on now i have run into a simular problem here is the code

                       if AlarmLo[X] != "x":
				if float(Probe1) < AlarmLo[X]:
					Resend = int(time.time()) - int(AlarmSent[X])
					if Resend > int(settings["MailResend"]):
						#SendMail("Probe %d Low Temp" % countX)
						print "Probe %d Low Temp" % X 
						AlarmSent = {X:int(time.time())}
			X = X + 1

The problem is the "float(Probe1)" it would need to be float(Probe(X+1)) ish not a static number must be between 1 and 6 hence the +1

 

Thanks

  • 0

Another note is that iterating like that is not considered "Pythonic".

Rather than:

x = 1
while x < 6:
    fn()
    x = x + 1

you should do:

for x in range(1, 6):
    fn()

(And possibly use xrange instead of range depending on the version of Python you are using)

  • Like 2
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.