Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I wanted to do a code that takes in an integer input, then finds if it is divisible by a two-digit prime number. If it is, it returns True, otherwise False. I already have made a function that identifies whether a number is prime or not called isPrime (which I used here):

num=int(input())
for i in range (10,num):
    if isPrime(i,2):
        if num%i==0:
            print(True)
        else:
            print(False)

I know that this prints all of the True's and False's from 10 to the inputted integer, so what I wanted to ask is how do I edit this so that if one of those outputs is True, it will only output True, but if none of those are True, it will output False?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
3.7k views
Welcome To Ask or Share your Answers For Others

1 Answer

Use any:

num=int(input())
print(any(num%i==0 for i in range (10,num) if isPrime(i,2)))

any returns True when it finds the first "Truthy" value in an iterable otherwise False


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...