首页 > 解决方案 > 给定范围内所有数字的总和除以 3 和 5,并将数字范围显示为列表

问题描述

def main(start,end):

    range(start,end)
    total=0
    for nums in range(start,end):
        if nums%3 ==0 or nums%5==0:
            total=(total)+(nums)
    return total

print(main(int(input("enter start ")),int(input("enter end ")))) 

如果我输入 1 到 100 答案将是 2318

如何在不重新输入的情况下打印数字范围列表?

print(list(range(start,end))) 

例如我想要[1,2,3,4,...,99]但没有输入但使用上面给出的原始输入

标签: pythonlistrange

解决方案


这是帮助你

def main(start,end):
    total=0
    l = []
    for nums in range(start,end):
        l.append(nums)

        if nums%3 ==0 or nums%5==0:
            total=(total)+(nums)
    print(l)

    return total


print(main(int(input("enter start ")),int(input("enter end "))))

输出:

enter start 1
enter end 100
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 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]
2318

推荐阅读