[Tutor] optional else with while

Manprit Singh manpritsinghece at gmail.com
Sun Oct 11 13:07:39 EDT 2020


Dear sir ,

Consider a problem where I have to write a program that  accepts a user
input (integer) and find its largest factor . If the number is a prime
number the program must display " It is a prime number". I have written a
program that is not valid for user input of 0 and 1 . The basic purpose is
to understand how an optional else clause works with  while loop  .

n = int(input("Enter any number"))
initial = n//2
while initial > 1:
    if n % initial == 0:
        print("Greatest factor of",n,"is",initial)
        break
    initial -= 1
else:
    print("It is a prime number")

Need your comments

Regards
Manprit Singh


More information about the Tutor mailing list