Issue
What is the best way to fix this? Please help.
Here is my Code.
if command == '/enable':
filename='line_en.xml'
else:
if command == '/disable':
filename='line_dis.xml'
with open(filename) as f:
data = f.read().replace('\n', '').replace('\r', '').encode()
response = requests.put('http://www.xxxx.com')
with open(filename) as f:
^^^^^^^^
UnboundLocalError: cannot access local variable 'filename' where it is not associated with a value
Solution
Use match case
advantage over if-elif-else.
snippet:
command = '/enable'
match command:
case '/enable':
filename = 'line_en.xml'
case '/disable':
filename = 'line_dis.xml'
with open(filename) as f:
data = f.read().replace('\n', '').replace('\r', '').encode()
response = requests.put('http://www.xxxx.com')
Answered By - toyota Supra
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.