所以我正在练习一些Python,该程序接受多个int输入,将其存储在列表中,然后将它们打印出来并在每个注释上进行注释。
rnumber = 500
perclist = [int(e) for e in input("Enter the percentages you want to comment: (separated by spaces) ").split()]
listlen = len(perclist)
def percentage(a, b):
return 100 * (float(a) / float(b))
def comment(a, b):
if percentage(a, b) <= 29:
print("That is a low percentage")
elif percentage(a, b) >= 30 and percentage(a, b) <= 69:
print("That is a medium percentage")
elif percentage(a, b) >= 70:
print("That is a high percentage")
listlen = str(listlen)
print("Total inputs: " + listlen)
percprint = [(e*5) for e in perclist]
print(percprint)
for e in percprint:
comment(e, rnumber)
[从技术上讲,它没有显示任何错误,但是我知道它无法正常工作,因为每当我输入14个以上(或大约14个数字)时,它总是“忘记”对某些数字应用注释,因此,假设我编写了25个输入,它只打印17的注释。我想我在这里想念什么,您能帮我吗?
更新:我发现了这个错误;问题出在comment
。我更改了条件句的参数,并对其进行了修复。正如您中某些人所指出的那样,显然这些函数未将函数应用于某些结果,因为这些百分比超出了条件范围。感谢你们每个人的帮助!
从代码开始,并且没有明显的失败的测试用例场景,John Coleman在注释中提到了可能发生的情况,这是您在comment
函数中测试范围的一个漏洞。连同一些代码清理,这就是我要写的内容:
def percentage(a, b):
return 100 * (float(a) / float(b))
def comment(a, b):
if percentage(a, b) <= 29:
print("That is a low percentage")
elif percentage(a, b) <= 69: # notice the range from above 29
print("That is a medium percentage")
elif percentage(a, b) > 69: # notice the range from above 69
print("That is a high percentage")
def main():
rnumber = 500
perclist = [int(e) for e in input("Enter the percentages you want to comment: (separated by spaces) ").split()]
percprint = [(e*5) for e in perclist]
print("Total inputs: " + len(perclist))
print(percprint)
for e in percprint:
comment(e, rnumber)
main()
如果您想根据评论添加进度计数器,则可以执行以下操作:
def comment(a, b, i):
if percentage(a, b) <= 29:
print("[%s] That is a low percentage" % i)
elif 29 < percentage(a, b) <= 69: # notice the range from above 29
print("[%s] That is a medium percentage" % i)
elif percentage(a, b) > 69: # notice the range from above 69
print("[%s] That is a high percentage" % i)
并且将enumerate
中的main()
用作:
for i, e in enumerate(percprint):
comment(e, rnumber, i+1)
[如果-相反,您想包含要注释的值,则可以执行以下操作:
def comment(a, b):
if percentage(a, b) <= 29:
print("[%s] That is a low percentage" % a)
elif 29 < percentage(a, b) <= 69: # notice the range from above 29
print("[%s] That is a medium percentage" % a)
elif percentage(a, b) > 69: # notice the range from above 69
print("[%s] That is a high percentage" % a)
[main()
与该答案的第一个版本相同。
如果您仅将整数用作输入,则您的代码完全没问题,但是如果您也想考虑浮点数,则可以尝试以下代码。它会为所有人运行
def percentage(a, b):
return 100 * (float(a) / float(b))
def comment(a, b):
if percentage(a, b) <= 29:
print("That is a low percentage")
elif percentage(a, b) <= 69:
print("That is a medium percentage")
else:
print("That is a high percentage")
if __name__ == '__main__':
rnumber = 500
perclist = [float(e) for e in input("Enter the percentages you want to comment: (separated by spaces) ").split()]
print(perclist)
listlen = len(perclist)
listlen = str(listlen)
print("Total inputs: " + listlen)
percprint = [(e*5) for e in perclist]
print(percprint)
for e in percprint:
comment(e, rnumber)