【发布时间】:2018-11-26 21:50:22
【问题描述】:
这是我要运行的部分。它起作用了,然后它开始搞砸了。基本上它意味着迭代一个 for 循环,如果 inptype != sale 那么它意味着跳过循环的迭代并继续下一个循环。但是,当值不等于该语句时,它应该跳到 else 语句并继续,但它没有,它遵循 inptype != sale 的过程,即使值等于 sale。
这是我的代码
for row in reader:
inpname = str(row[15])
inptype = str(row[19])
print(inptype)
print (inpname)
if not row: # if row is blank
print("not row")
continue# continue loop on next iteration of for loop
elif "CUSTOMER DISCOUNT" in inpname:
print("customer dis")
continue
elif inptype != "Sale" or "sale" or "SALE":
continue
else:
【问题讨论】:
-
不可能说出任何带有混乱意图的东西 - 你需要解决这个问题,否则没有人可以提供帮助。另外,这东西不太可能跑了,在最后一个
else:之后没有预期的块@ -
inpname 和 inptype 应该是单个字符吗?您索引行的方式只会返回一个字母。也许研究 python 字符串切片?
-
另外,只是为了让您知道条件语句:
inptype != "Sale" or "sale" or "SALE"将永远是True。你可能想写:inptype != "Sale" and inptype != "sale" and inptype != "SALE" -
@rammelmueller 代码运行得非常好,只要我添加了带有多个条件的 elif 语句。我显然没有正确格式化它,但其余代码已经运行了一个多月。我知道缩进完全关闭,在我的 IDLE 中它运行正常,只是当我将它粘贴到 StackOverflow 时它搞砸了
标签: python-3.x for-loop if-statement