不输出列表中的每个值
Not outputting every value from list
当我打印结果时,它只为每个函数打印一个 True/False 值。对于 dict1 和 dict2,它应该重复 return 多个值。
当前结果是:
False
True
False
预期结果是:
打印(fullCoverageClearance(dict1)) -> [False, False]
print(fullCoverageClearance(dict2)) -> [False, True, False, True]
print(fullCoverageClearance(dict3)) -> [False]
代码如下:
dict1 = {"JS" : {"Paid" : [200, 400, 500, 600]}, "SK" : {"Paid": [400, 1000, 1600]}}
dict2 = {"JS" : {"Paid" : [200, 400, 500, 600]}, "SK" : {"Paid": [1010, 2000]}, "MJ":{"Paid" : [5, 6, 7]}, "ZF" : {"Paid": [2660, 500]}}
dict3 = {"SK" : {"Paid" : [200, 400]}}
def fullCoverageClearance(dict1):
total1 = [sum(dict1["JS"]["Paid"]), sum(dict3["SK"]["Paid"])]
for i in total1:
if i <= 3000:
return 'False'
else:
return 'True'
def fullCoverageClearance(dict2):
total2 = [sum(dict2["JS"]["Paid"]), sum(dict2["SK"]["Paid"]), sum(dict2["MJ"]["Paid"]), sum(dict2["ZF"]["Paid"])]
for i in total2:
if i <= 3000:
return 'False'
else:
return 'True'
def fullCoverageClearance(dict3):
total3 = [sum(dict3["SK"]["Paid"])]
for i in total3:
if i <= 3000:
return 'False'
else:
return 'True'
print(fullCoverageClearance(dict1))
print(fullCoverageClearance(dict2))
print(fullCoverageClearance(dict3))
阅读你的代码,它混淆了你试图完成的事情。也许这会让您走上正确的道路:
dict1 = {"JS" : {"Paid" : [200, 400, 500, 600]}, "SK" : {"Paid": [400, 1000, 1600]}}
def fullCoverageClearance(vals):
totals = [sum(vals["JS"]["Paid"]), sum(vals["SK"]["Paid"])]
print(totals)
results = []
for amt in totals:
print(amt)
if amt <= 3000:
results.append(False)
else:
results.append(True)
return results
print(fullCoverageClearance(dict1))
# Output
# [1700, 3000]
# 1700
# 3000
# [False, False]
更好的方法是将 fullCoverageClearance()
概括为 re-usable
dict1 = {"JS" : {"Paid" : [200, 400, 500, 600]}, "SK" : {"Paid": [400, 1000, 1600]}}
dict2 = {"JS" : {"Paid" : [200, 400, 500, 600]}, "SK" : {"Paid": [1010, 2000]}, "MJ":{"Paid" : [5, 6, 7]}, "ZF" : {"Paid": [2660, 500]}}
dict3 = {"SK" : {"Paid" : [200, 400]}}
def getFullCoverageClearance(data):
results = []
for key, val in data.items():
total = sum(val['Paid'])
if total <= 3000:
results.append(False)
else:
results.append(True)
return results
print(getFullCoverageClearance(dict1))
print(getFullCoverageClearance(dict2))
print(getFullCoverageClearance(dict3))
# Output
# [False, False]
# [False, True, False, True]
# [False]
如果我没看错问题,这就是你想要的,我没有更改条件,这意味着实际输出是这样的:
[False, False]
[False, True, False, True]
[False]
第一个输出为 false 和 false 的原因是第二个值恰好是 3000,它将条件评估为 true 输出 false
dict1 = {"JS" : {"Paid" : [200, 400, 500, 600]}, "SK" : {"Paid": [400, 1000, 1600]}}
dict2 = {"JS" : {"Paid" : [200, 400, 500, 600]}, "SK" : {"Paid": [1010, 2000]}, "MJ":{"Paid" : [5, 6, 7]}, "ZF" : {"Paid": [2660, 500]}}
dict3 = {"SK" : {"Paid" : [200, 400]}}
def fullCoverageClearance1(dict1):
total1 = [sum(dict1["JS"]["Paid"]), sum(dict1["SK"]["Paid"])]
results = []
for i in total1:
if i <= 3000:
results.append(False)
else:
results.append(True)
return results
def fullCoverageClearance2(dict2):
total2 = [sum(dict2["JS"]["Paid"]), sum(dict2["SK"]["Paid"]), sum(dict2["MJ"]["Paid"]), sum(dict2["ZF"]["Paid"])]
results = []
for i in total2:
if i <= 3000:
results.append(False)
else:
results.append(True)
return results
def fullCoverageClearance3(dict3):
total3 = [sum(dict3["SK"]["Paid"])]
results = []
for i in total3:
if i <= 3000:
results.append(False)
else:
results.append(True)
return results
print(fullCoverageClearance1(dict1))
print(fullCoverageClearance2(dict2))
print(fullCoverageClearance3(dict3))
如果您使用 return,整个函数将退出并处理仅输出一个值的下一个函数。
此外,您的函数名称重叠,覆盖了之前声明的函数。
def fullCoverageClearance(dict1):
total1 = [sum(dict1["JS"]["Paid"]), sum(dict3["SK"]["Paid"])]
for i in total1:
if i <= 3000:
return 'False'
else:
return 'True'
如果您查看变量 total1 第二个元素,您会发现有错别字。请务必仔细检查您的代码。
当我打印结果时,它只为每个函数打印一个 True/False 值。对于 dict1 和 dict2,它应该重复 return 多个值。
当前结果是:
False
True
False
预期结果是:
打印(fullCoverageClearance(dict1)) -> [False, False]
print(fullCoverageClearance(dict2)) -> [False, True, False, True]
print(fullCoverageClearance(dict3)) -> [False]
代码如下:
dict1 = {"JS" : {"Paid" : [200, 400, 500, 600]}, "SK" : {"Paid": [400, 1000, 1600]}}
dict2 = {"JS" : {"Paid" : [200, 400, 500, 600]}, "SK" : {"Paid": [1010, 2000]}, "MJ":{"Paid" : [5, 6, 7]}, "ZF" : {"Paid": [2660, 500]}}
dict3 = {"SK" : {"Paid" : [200, 400]}}
def fullCoverageClearance(dict1):
total1 = [sum(dict1["JS"]["Paid"]), sum(dict3["SK"]["Paid"])]
for i in total1:
if i <= 3000:
return 'False'
else:
return 'True'
def fullCoverageClearance(dict2):
total2 = [sum(dict2["JS"]["Paid"]), sum(dict2["SK"]["Paid"]), sum(dict2["MJ"]["Paid"]), sum(dict2["ZF"]["Paid"])]
for i in total2:
if i <= 3000:
return 'False'
else:
return 'True'
def fullCoverageClearance(dict3):
total3 = [sum(dict3["SK"]["Paid"])]
for i in total3:
if i <= 3000:
return 'False'
else:
return 'True'
print(fullCoverageClearance(dict1))
print(fullCoverageClearance(dict2))
print(fullCoverageClearance(dict3))
阅读你的代码,它混淆了你试图完成的事情。也许这会让您走上正确的道路:
dict1 = {"JS" : {"Paid" : [200, 400, 500, 600]}, "SK" : {"Paid": [400, 1000, 1600]}}
def fullCoverageClearance(vals):
totals = [sum(vals["JS"]["Paid"]), sum(vals["SK"]["Paid"])]
print(totals)
results = []
for amt in totals:
print(amt)
if amt <= 3000:
results.append(False)
else:
results.append(True)
return results
print(fullCoverageClearance(dict1))
# Output
# [1700, 3000]
# 1700
# 3000
# [False, False]
更好的方法是将 fullCoverageClearance()
概括为 re-usable
dict1 = {"JS" : {"Paid" : [200, 400, 500, 600]}, "SK" : {"Paid": [400, 1000, 1600]}}
dict2 = {"JS" : {"Paid" : [200, 400, 500, 600]}, "SK" : {"Paid": [1010, 2000]}, "MJ":{"Paid" : [5, 6, 7]}, "ZF" : {"Paid": [2660, 500]}}
dict3 = {"SK" : {"Paid" : [200, 400]}}
def getFullCoverageClearance(data):
results = []
for key, val in data.items():
total = sum(val['Paid'])
if total <= 3000:
results.append(False)
else:
results.append(True)
return results
print(getFullCoverageClearance(dict1))
print(getFullCoverageClearance(dict2))
print(getFullCoverageClearance(dict3))
# Output
# [False, False]
# [False, True, False, True]
# [False]
如果我没看错问题,这就是你想要的,我没有更改条件,这意味着实际输出是这样的:
[False, False]
[False, True, False, True]
[False]
第一个输出为 false 和 false 的原因是第二个值恰好是 3000,它将条件评估为 true 输出 false
dict1 = {"JS" : {"Paid" : [200, 400, 500, 600]}, "SK" : {"Paid": [400, 1000, 1600]}}
dict2 = {"JS" : {"Paid" : [200, 400, 500, 600]}, "SK" : {"Paid": [1010, 2000]}, "MJ":{"Paid" : [5, 6, 7]}, "ZF" : {"Paid": [2660, 500]}}
dict3 = {"SK" : {"Paid" : [200, 400]}}
def fullCoverageClearance1(dict1):
total1 = [sum(dict1["JS"]["Paid"]), sum(dict1["SK"]["Paid"])]
results = []
for i in total1:
if i <= 3000:
results.append(False)
else:
results.append(True)
return results
def fullCoverageClearance2(dict2):
total2 = [sum(dict2["JS"]["Paid"]), sum(dict2["SK"]["Paid"]), sum(dict2["MJ"]["Paid"]), sum(dict2["ZF"]["Paid"])]
results = []
for i in total2:
if i <= 3000:
results.append(False)
else:
results.append(True)
return results
def fullCoverageClearance3(dict3):
total3 = [sum(dict3["SK"]["Paid"])]
results = []
for i in total3:
if i <= 3000:
results.append(False)
else:
results.append(True)
return results
print(fullCoverageClearance1(dict1))
print(fullCoverageClearance2(dict2))
print(fullCoverageClearance3(dict3))
如果您使用 return,整个函数将退出并处理仅输出一个值的下一个函数。
此外,您的函数名称重叠,覆盖了之前声明的函数。
def fullCoverageClearance(dict1):
total1 = [sum(dict1["JS"]["Paid"]), sum(dict3["SK"]["Paid"])]
for i in total1:
if i <= 3000:
return 'False'
else:
return 'True'
如果您查看变量 total1 第二个元素,您会发现有错别字。请务必仔细检查您的代码。