我是 Python 初学者。我正在尝试编写一些基本代码来自动化一个简单但重复的工作过程,但被这个问题难住了。
# Prior to the code block below, a csv file import would have created the a and ag variables with their respective numerical values.
high = list(range(66, 99))
mod = list(range(36, 65))
low = list(range(1,35))
verysiggap = list(range(61, 99))
siggap = list(range(31, 60))
nonsiggap = list(range(1, 30))
fco_output_list = []
def fco_block1(*args):
if a in high and ag in high:
print("Both a and ag are high!")
fco_output_list.append("fco1")
if a in high and ag in high and a-ag in nonsiggap:
fco_output_list.append("foc2")
fco_block1(a, ag)
a_ag_combi_output = 0
a_ag_gap_output = 0
def fco_block2(*args):
global a_ag_combi_output
if a in high and ag in high:
a_ag_combi_output = 0
elif a in high and ag in low or ag in mod:
a_ag_combi_output = 1
elif a in mod and ag in low:
a_ag_combi_output = 2
elif ag in high and a in low or a in mod:
a_ag_combi_output = 3
elif ag in mod and a in low:
a_ag_combi_output = 4
elif a in low and ag in low:
a_ag_combi_output = 5
global a_ag_gap_output
if a in high and ag in high and a-ag in nonsiggap:
a_ag_gap_output = 1
fco_block2(a, ag)
# There are some simple print functions after the code above that tell me very clearly that fco_block2 executed without any issues.
至于 fco_block1 的执行,没有错误消息,但条件似乎没有执行。根据导入的 a 和 ag 值,fco_output_list 应该由 fco1 和 fco2 填充,但事实并非如此。
为什么会这样?我觉得这很奇怪,尤其是当 block1 中的条件语句的措辞与 block2 中的相应条件相同,但是 block2 中的那些被执行而不是 block1 中的那些?
您的问题似乎与您定义“高”列表的方式有关。
根据您的评论:“我用相同的 csv 文件运行了无数次(其中 a = 99,ag = 86) ”
在 Python 中,“range”的最终值是排他的(即最多但不包括),这意味着“list(range(66, 99))”将以 98 结尾。为了清楚起见,这些都是“高”名单:
[66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98]
如您所见,99 不存在。因此,例如,(a = 99, ag = 86) 将失败,因为 a 不在“高”状态。
我用 (a = 70, ag = 67) 测试了你的代码,它打印出“a 和 ag 都很高!”
如果要(a=99,ag=86)触发fco_block1中的print语句,把high改成
high = list(range(66, 100))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句