mardi 22 mars 2016

How would you code an "only if" condition in python

I can't seem to figure out how I would code something like an "only if" condition. For this example, I want to print if the user's input is a prime number only if the number is divisible by 1 or itself. I feel like it has something to do with the iterations of the for loop I have. Any help would be greatly appreciated. Thanks!

number = int(input("Enter a positive number to test: "))

while number <= 0:
    print ("Sorry, only positive numbers. Try again.")
    number = int(input("Enter a positive number to test: "))

test = 2

number1 = number - 1
for x in range (0, number1):
    trial = number % test
    if trial != 0:
        print (test, "is NOT a divisor of", number, "...")
    else:
        print (test, "is a divisor of", number, "...")
    test = test + 1

Aucun commentaire:

Enregistrer un commentaire