Python: To Check For Prime And Increment
i have exactly 5 days of practise, an hour daily so kindly forgive if my questions are very low level. No prior coding exp My objective of the below code is 1- check if entered num
Solution 1:
The way you have done has a lot of errors not efficient. By making some modification to your code i have made it much simpler. Read the comments to understand:
def primetest (num): # check if number is a prime
return(all(num % i for i in range(2, num)))
num = int (input ("enter a number:")) # main code:
while True: #loop continues until prime number found(True)
if primetest(num):
print(num,"is a prime number.")
break #(stops loop if prime found)
else: #otherwise repeat by incrementing to the next num until found
print(num,"is not a prime number.")
num += 1
Output:
enter a number:45
45 is not a prime number.
46 is not a prime number.
47 is a prime number.
Post a Comment for "Python: To Check For Prime And Increment"