- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我为我的 A Level 计算任务编写了一个侦察系统。该程序旨在将球探的信息存储在球探小屋中,包括徽章、排行榜系统以及用于从列表中添加/查找/删除球探的管理系统。侦察信息必须存储在文件中。
删除函数的文件处理过程(我的问题所在):删除侦察按钮触发弹出窗口(使用 tkinter)。该窗口收集侦察员的 ID,然后搜索侦察员文件,扫描存储的侦察员 ID 并将其与输入的 ID 进行比较。如果找到 ID,它将跳过文件的这一行,否则将该行复制到临时文件。完成所有行后,temp 中的行将复制到原始文件的新空白版本,并删除/重新创建临时文件为空白。
我的问题:问题是当程序将要删除的 ID (remID) 与文件中当前正在查看的侦察员的 ID (sctID) 进行比较时,它会返回 false,而实际上它们是相等的。这可能是我对变量的处理、我拆分行以获取 ID 甚至我的数据类型的问题。我只是不知道。我尝试将两者都转换为字符串,但仍然是错误的。此部分的代码如下。提前致谢!
elif self._name == "rem":
remID = str(scoutID.get())
if remID != "":
#store all the lines that are in the file in a temp file
with open(fileName,"r") as f:
with open(tempFileName,"a") as ft:
lines = f.readlines()
for line in lines:
sctID = str(line.split(",")[3])
print("%s,%s,%s"%(remID, sctID, remID==sctID))
#print(remID)
if sctID != remID: #if the ID we are looking to remove isn't
#the ID of the scout we are currently looking at, move it to the temp file
ft.write(line)
#remove the main file, then rectrate a new one
os.remove(fileName)
file = open(fileName,"a")
file.close()
#copy all the lines back to the main file
with open(tempFileName,"r") as tf:
lines = tf.readlines()
with open(fileName,"a") as f:
for line in lines:
f.write(line)
#finally, delete and recreate the temp file
os.remove(tempFileName)
file = open(tempFileName,"a")
file.close()
#remove the window
master.destroy()
我的输出:
1,1
,False
1,2
,False
1,3
,False
最佳答案
通过转换为字符串,您可以隐藏错误。
出于调试目的,始终尝试使用 repr(value) 而不是 str(value)。您还应该知道,最好比较整数而不是字符串——例如“1”!=“1”。
Edit: From your output, it is clear that you have an extra '\n' (Newline) in the sctID. Because you compare strings, this will be always be False.
我想,您可能有带有额外空格或其他隐藏字符的字符串,或者只是不同类型的值,这也会导致不同的结果。
关于Python 向 "1"= ="1"返回 false。任何想法为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30923049/
class test { public static void main(String[] args){ Object o1 = new Object(); O
我以为我理解了 Python 中的这两个单例值,直到我看到有人在代码中使用 return l1 or l2,其中 l1 和 l2 都是链表对象,并且(s)他想如果不为 None 则返回 l1,否则返回
这个问题在这里已经有了答案: Why does the expression 0 >> (True == False) is False True >>> True == (False is Fals
为什么在 Python 中它是这样评估的: >>> False is False is False True 但是当用括号尝试时表现如预期: >>> (False is False) is False
我有一个名为“apple”的表,我编写了以下查询: select name, count(name), case when istasty is null then fal
python boolean 逻辑中的运算符优先级 print(False==True or False) #answer is True print(False==(False or True))#
请不要看条件,因为它们在这里是为了便于理解行为 为什么 result 等于 true ? boolean result = false && (false)?false:true; 我知道我们可以通过
乍一看,这篇文章可能看起来像是重复的,但事实并非如此。相信我,我已经查看了所有 Stack Overflow,但都无济于事。 无论如何,我从 Html.CheckBoxFor 得到了一些奇怪的行为。
这个问题在这里已经有了答案: python operator precedence of in and comparison (4 个答案) 关闭 6 年前。 我的一位前辈演示了它,我想知道这是否是
我最近参加了 Java 的入门测试,这个问题让我很困惑。完整的问题是: boolean b1 = true; boolean b2 = false; if (b2 != b1 != b2) S
为什么 {} == false 评估为 false 而 [] == false 评估为 true在 javascript 中? 最佳答案 这是根据 Abstract Equality Comparis
这个问题在这里已经有了答案: Why does (1 in [1,0] == True) evaluate to False? (1 个回答) 关闭7年前。 为什么使用括号时这些语句按预期工作: >>
我试过搜索这个,但我真的不知道如何表达它以查看是否有其他人发布了答案。 但是,我正在制作一个国际象棋游戏和一个人工智能来配合它,这是非常困难的,我的问题是当我检查两个棋子是否在同一个团队时我必须做 (
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
为什么 为 false || null 返回与 null || 不同的结果错误? 我可以安全地依赖 return myVar || false 如果 myVar 为 null 或 false,则返回
我正在尝试遵循 NHibernate 教程,“你的第一个基于 NHibernate 的应用程序:修订 #4”在 NHibernate Forge。 但线路:new SchemaExport(cfg).
这个问题在这里已经有了答案: Empty list boolean value (3 个答案) 关闭 4 年前。 我是 Python 的新手,不理解以下行为: 为什么要声明 [] == False
以下函数循环访问对象的值。如果值为空this.hasInvalidValue设置为true ,如果不为空 this.hasInvalidValue设置为false : user: { email:
所以我正在玩 java.lang.reflect 东西并尝试制作类似 this 的东西。这是我的问题(可能是一个错误): 将字段设置为 true 的方法的代码: private static void
当我在编程时,我的 if 语句出现了意想不到的结果。 这个代码警报怎么会是真的?我在 W3S 没有找到任何可以帮助我的东西,我真的很想知道为什么这些警报是“正确的” window.alert(fals
我是一名优秀的程序员,十分优秀!