尝试编写多个脚本,这些脚本将访问一个文本文件,从所述文本文件检索其切换值,然后返回值为false并将其更改为true,反之亦然。你知道吗
值总是返回为false,不确定缺少什么,但下面是文本文件:
1
False
3
True
5
6
7
8
9
以下是源代码:
def append():
with open('values', 'r') as file:
# read a list of lines into data
data = file.readlines()
# now change the line
re_value = value
data[1] = re_value+"\n"
# and write everything back
with open('values', 'w') as file:
file.writelines(data)
print("value changed to "+re_value)
def read() -> bool:
#opens the value file to find the value of the toggle
f=open("values", "r")
for x, line in enumerate(f):
if x == 1:
toggle = line
print(toggle)
return toggle
f.close()
toggle = read()
if toggle:
print("Light is on turn it off")
#runs command to turn off the light
#runs command to change value for light
value = "False"
append()
else:
print("Light is off turn it on")
#runs command to turn on the light
#runs command to change value for light
value = "True"
append()
尝试以下切换:
您用来计算条件的布尔值似乎是一个字符串。你知道吗
在这种情况下,可以将其保留为字符串:
或者实现类似的功能:
发现我的错误,伙计们,谢谢你们的帮助,把if语句改为
它缺少新行标记,因为它是一个多行文本文档
相关问题 更多 >
编程相关推荐