how to set screen brightness automatically depending on battery percentage using python

import psutil
import screen_brightness_control as screen
while True:
    battery =  psutil.sensors_battery()
    if battery.power_plugged == True:
        screen.set_brightness(50)
    elif battery.percent >= 30 and battery.power_plugged == False:
        screen.set_brightness(0)

Here is what the above code is Doing:
1. It’s importing the psutil and screen_brightness_control modules.
2. It’s creating an infinite loop.
3. It’s storing the battery status in the battery variable.
4. It’s checking if the power is plugged in. If it is, it sets the screen brightness to 50%.
5. If the power is not plugged in and the battery is above 30%, it sets the screen brightness to 0%.