看不到一个函数的输出等于一个字符串
Can't see the output of a function is equal to a string
我有一个程序,只要我按下一个按钮,它就会将一个值写入一个文本文件。下次按下按钮时,它会找到最后写入的字符串并调用变量来执行相反的操作。这一直是 运行,直到我将其关闭。我的问题是它不会停止打印“Off”,这是我的文本文件中的最后一个字符串。当程序运行时,Fine() 打印文本文件的最后一个字符串,然后进入 while 循环。它似乎无法识别我的 if 语句,只是再次打印 Fine() 而不做任何更改。我不知道为什么它一直打印“关闭”这可能是我不知道的格式化问题,但我已经看过但找不到类似的东西。
import time
import os
# this is the system that detects the last line. In order to seek the end of file, we have to open the file in binary mode. We seek the last 2 characters
# since the file is likely to end with a newline. Then, keep reading a single byte until a newline character is found. And, finally, we use readline() to read the \
# final line, and decode() to convert from bytes to string.
# Checker is a fall back system so if there is nothing in the file then it writes on(or the terms i am using at that time.) so that when I press the button again it starts the cycle.
def Checker():
file = open("Store.txt").readlines()
#print(file[0:2])
if file[0:2] != ['On\n', 'Off']:
print("nothing is here...Restarting file with added Off")
with open("Store.txt","w") as df:
df.write("On\nOff")
df.close()
def Fine():
with open("Store.txt", "rb") as f:
f.seek(-2, os.SEEK_END)
while f.read(1) != b'\n':
f.seek(-2, os.SEEK_CUR)
print(f.readline().decode())
def w_on():
print("The light is on")
with open("Store.txt", "w") as on:
on.write("On\n","On")
on.close()
def w_off():
print("this is off")
with open ("Store.txt", "w") as off:
off.write("off\no","OFFF")
off.close()
#This checks the status of the text file and corrects it if the text inside is wrong.
Checker()
# this prints the current last line in the Store.txt file.
Fine()
#============================================================================================
#Here is the start of my Issue of it printing "Off"
# 1==1 replaces the button.pressed() function
while True:
time.sleep(1)
if 1==1:
if Fine()==("On"):
# the print function replaces the function for the servo moving. right here the off funtion would happen.
print("The light is turning off")
w_off()
elif Fine()== ("Off"):
# the print fucntion replaces the function for the serveo moving. right below, the on funtion would happen.
print("The light is turning on")
w_on()
time.sleep(2)
就是这一行:print(f.readline().decode())
所以在你第一次执行时,你用 On
和 Off
编写了文件,并用 Off
作为文件中的最后一条语句。
现在每次在 while 循环中调用 Fine()
时,上面的语句都会打印最后一行,即当前的 Off
。
你的 Fine()
调用是 returning None
(因为你的函数没有 return 任何东西),因此你的 if
和 elif
除了调用 Fine()
来检查它的值外,并没有真正执行。所以没有任何内容写入您的文件,您不断得到最后一行仍然是 Off
.
我更改了 if 语句正在读取的变量,我能够解决这个问题。
# 1==1 replaces the button.pressed() function
while True:
time.sleep(1)
with open("Store.txt", "rb") as f:
f.seek(-2, os.SEEK_END)
while f.read(1) != b'\n':
f.seek(-2, os.SEEK_CUR)
# Adeed the var Detect so I can tell the output of the reading.
Detect = f.readline().decode()
f.close()
# Now it asks what the output of the reading is not the output of a function, so it continues through instead of returning a None.
if Detect==("On"):
# the print function replaces the function for the servo moving. right here the off function would happen.
print("The light is turning off")
w_off()
elif Detect== ("Off"):
# the print fucntion replaces the function for the serveo moving. right below, the on funtion would happen.
print("The light is turning on")
w_on()
else:
print("Incorrect")
time.sleep(2)
我有一个程序,只要我按下一个按钮,它就会将一个值写入一个文本文件。下次按下按钮时,它会找到最后写入的字符串并调用变量来执行相反的操作。这一直是 运行,直到我将其关闭。我的问题是它不会停止打印“Off”,这是我的文本文件中的最后一个字符串。当程序运行时,Fine() 打印文本文件的最后一个字符串,然后进入 while 循环。它似乎无法识别我的 if 语句,只是再次打印 Fine() 而不做任何更改。我不知道为什么它一直打印“关闭”这可能是我不知道的格式化问题,但我已经看过但找不到类似的东西。
import time
import os
# this is the system that detects the last line. In order to seek the end of file, we have to open the file in binary mode. We seek the last 2 characters
# since the file is likely to end with a newline. Then, keep reading a single byte until a newline character is found. And, finally, we use readline() to read the \
# final line, and decode() to convert from bytes to string.
# Checker is a fall back system so if there is nothing in the file then it writes on(or the terms i am using at that time.) so that when I press the button again it starts the cycle.
def Checker():
file = open("Store.txt").readlines()
#print(file[0:2])
if file[0:2] != ['On\n', 'Off']:
print("nothing is here...Restarting file with added Off")
with open("Store.txt","w") as df:
df.write("On\nOff")
df.close()
def Fine():
with open("Store.txt", "rb") as f:
f.seek(-2, os.SEEK_END)
while f.read(1) != b'\n':
f.seek(-2, os.SEEK_CUR)
print(f.readline().decode())
def w_on():
print("The light is on")
with open("Store.txt", "w") as on:
on.write("On\n","On")
on.close()
def w_off():
print("this is off")
with open ("Store.txt", "w") as off:
off.write("off\no","OFFF")
off.close()
#This checks the status of the text file and corrects it if the text inside is wrong.
Checker()
# this prints the current last line in the Store.txt file.
Fine()
#============================================================================================
#Here is the start of my Issue of it printing "Off"
# 1==1 replaces the button.pressed() function
while True:
time.sleep(1)
if 1==1:
if Fine()==("On"):
# the print function replaces the function for the servo moving. right here the off funtion would happen.
print("The light is turning off")
w_off()
elif Fine()== ("Off"):
# the print fucntion replaces the function for the serveo moving. right below, the on funtion would happen.
print("The light is turning on")
w_on()
time.sleep(2)
就是这一行:print(f.readline().decode())
所以在你第一次执行时,你用 On
和 Off
编写了文件,并用 Off
作为文件中的最后一条语句。
现在每次在 while 循环中调用 Fine()
时,上面的语句都会打印最后一行,即当前的 Off
。
你的 Fine()
调用是 returning None
(因为你的函数没有 return 任何东西),因此你的 if
和 elif
除了调用 Fine()
来检查它的值外,并没有真正执行。所以没有任何内容写入您的文件,您不断得到最后一行仍然是 Off
.
我更改了 if 语句正在读取的变量,我能够解决这个问题。
# 1==1 replaces the button.pressed() function
while True:
time.sleep(1)
with open("Store.txt", "rb") as f:
f.seek(-2, os.SEEK_END)
while f.read(1) != b'\n':
f.seek(-2, os.SEEK_CUR)
# Adeed the var Detect so I can tell the output of the reading.
Detect = f.readline().decode()
f.close()
# Now it asks what the output of the reading is not the output of a function, so it continues through instead of returning a None.
if Detect==("On"):
# the print function replaces the function for the servo moving. right here the off function would happen.
print("The light is turning off")
w_off()
elif Detect== ("Off"):
# the print fucntion replaces the function for the serveo moving. right below, the on funtion would happen.
print("The light is turning on")
w_on()
else:
print("Incorrect")
time.sleep(2)