Hi all,
I have been working on a robot car for a while in an attempt to teach my son how to code. I have everything working but I am aware that my code is incredibly bad. Now that it is working I am trying to go back and redo parts to make it more efficient. I have read a lot about list (they seem useful) and am not trying to use one to average 3 Ultrasonic sensor readings. I did this originally using 3 variables adding them together and dividing by 3. I think I should be able to use a list of variables and a for loop to do the same thing but can not get it to work.
Code
Maybe I am assuming I can do something I can not. If there is a solution, please feel free to talk to me like I am 7, the last time I coded I had a line that said "goto line 10"
Thank you,
Gary
I have been working on a robot car for a while in an attempt to teach my son how to code. I have everything working but I am aware that my code is incredibly bad. Now that it is working I am trying to go back and redo parts to make it more efficient. I have read a lot about list (they seem useful) and am not trying to use one to average 3 Ultrasonic sensor readings. I did this originally using 3 variables adding them together and dividing by 3. I think I should be able to use a list of variables and a for loop to do the same thing but can not get it to work.
Code
def multi_signal_cleanup (trig, echo): # averages 3 signals from ultrasound samples = [s1, s2, s3] for variable in samples: variable = getSonar() sample_ave = (s1+s2+s3)/3 return sample_aveI get an error
Error:Program is starting...
Traceback (most recent call last):
File "/home/pi/mu_code/freenove us sensor program.py", line 61, in <module>
loop() #
File "/home/pi/mu_code/freenove us sensor program.py", line 53, in loop
distance = multi_signal_cleanup(20, 19) # get distance *3
File "/home/pi/mu_code/freenove us sensor program.py", line 44, in multi_signal_cleanup
samples = [s1, s2, s3]
NameError: name 's1' is not defined
>>>
I understand the error to have an undefined variable and I have tried to define it, but then it just has the value I attached to it and not the US distance.Maybe I am assuming I can do something I can not. If there is a solution, please feel free to talk to me like I am 7, the last time I coded I had a line that said "goto line 10"
Thank you,
Gary