简体   繁体   中英

PYTHON I am trying to break loop when found first number divisible by 16, and print results so I can check

I'm trying to code a function which returns the first number in the range (n1,n2) that is divisible by 16. If no number in the range is divisible by 16, I want to return 0. I've tried to code but I do not know how to print the results. I also want the code to break when the first value divisible by found.

def first_div_16(n1,n2):
    for i in range(n1, n2):  # type: int
        if i % 16 == 0:
            return i
        break
    else:
        return 0


(first_div_16(2,50))

You don't need the break statement since the return does the job.

def first_div_16(n1,n2):
    for i in range(n1, n2):  # type: int
        if i % 16 == 0:
            return i
    else:
        return 0


print(first_div_16(2,50))
#16

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM