Asking user for input – prime number code

Question:

num = 3    #take input from the user
if num > 1: # if input is greater than 1 check for factors
   for i in range(2,num):
       if (num % i) == 0:
               print(num,"is not a prime number")
               break
   else:
       print(num,"is a prime number")
            #if input number is less than or equal to 1, it is not prime

else:
   print(num,"is not a prime number")

I have my code here which works out the prime number of the num section, what I need to do it ask the user for the input when I run the code.

Asked By: Nakash

||

Answers:

This is pretty straightforward:

num = int(input("Input a number: "))
Answered By: ForceBru

Since input() always assumes that it’s taking a string ,you have to convert it to int :

print("Enter a number:")
num = int(input())   #this here
if num > 1: # if input is greater than 1 check for factors
   for i in range(2,num):
       if (num % i) == 0:
               print(num,"is not a prime number")
               break
   else:
       print(num,"is a prime number")
            #if input number is less than or equal to 1, it is not prime

else:
   print(num,"is not a prime number")
Answered By: Taufiq Rahman
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.