-1
我想在我的Python程序中使用一個變量,然後將其轉換爲JSON來控制我的智能燈。變量「bright」是JSON中燈號亮度的數字。我不確定如何包含它,因爲它不會發送,除非作爲字符串發送。我的代碼如下;在JSON中使用Python變量
import json
import requests
Light="http://192.168.1.102/api/F5La7UpN6XueJZUts1QdyBBbIU8dEvaT1EZs1Ut0/lights/5/state"
off='{"on":false}'
on='{"on":true}'
bri='{"bri":bright}'
def lights_off():
r = requests.put(Light,off)
def lights_on():
r = requests.put(Light,on,bright)
x=1
while x==1:
choice=input("Lights on or off?")
briChoice=input("What percentage brightness?")
bright=int((briChoice/100)*254)
if choice=="on":
lights_on()
elif choice=="off":
lights_off()
else:
print("Don't be stupid.")
choice2=input("Would you like to exit? Y/N").lower()
if choice2=="y":
break
謝謝你,偉大工程:) –