How can I rename selected objects instead of just the active object? I am trying to take all the selected objects and rename them based on their active material. I can make it work with the active object, but I can't figure out a way to do it with the selected objects.
This is what I have for the active object, that works as expected.

import bpy

#takes active object and renames it to the active material
name = bpy.context.active_object.active_material.name
bpy.context.object.name = name

Thanks in advance!
You can do something like this:
import bpy

for ob in bpy.data.objects:
    if ob.select_get():
        #ob.name=ob.material_slots[0].name #this can fail if no material is assigned
        print(ob.name)

Or using context as @batFINGER suggests
import bpy

for ob in bpy.context.selected_objects:
     #ob.name=ob.material_slots[0].name #this can fail if no material is assigned
     print(ob.name)