mercredi 10 octobre 2018

Use iterating and non-iterating 'if' statement under one 'for ... in' block

The goal of this code is to determine whether a number is prime or not, and if it isn't, prints the numbers that the given number can be divided by.

My question is: Is it possible to merge two 'for ... in' blocks into one, in the code presented below?

num = 224

list1 = []

for i in range(2, num):
    if num % i == 0:
        list1.append(i)

for i in range(2, num):
    if num % i == 0:
        print(num, 'is not prime and can be divided by the following numbers:\n', list1)
        break
else:
    print(num, 'is Prime.')

Aucun commentaire:

Enregistrer un commentaire