I have a coding assignment in which I need to ask the user to enter a number n, then output the prime number closest to n. If there are two primes equally close to n, then output the smaller of the two primes. Currently I have the code posted below which obviously isn't correct, but I can't get the correct solution.
n = int(input("enter a number: "))
x = n
y = n
for i in range(2, n):
while n % i == 0:
x += 1
y -= 1
if n % i != 0 and (x-n) < (n-y):
print("The prime number closest to", n, "is", x)
elif n % i != 0 and (n-y) < (x-n):
print ("The prime number closest to" , n, "is", y)
else:
print ("The prime number closest to" , n, "is", n)