Simple material swap python script not working

Hi. Thanks, that seemed have worked. although im getting this message. any idea how to rectify this?